Echo Command in Linux

Echo command mainly prints on the screen whatever you ask it. It a simple function but most the scripts would be incomplete with echo command. You won’t be able get visible output from the shell script, without echo command.

How to check what version of echo command you are using?

[root@server ~]# /bin/echo --version
echo (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Brian Fox and Chet Ramey.

How to print using echo command ?

it’s actually quite simple. Just use echo command following by the text.

[root@server ~]# echo "Hello JustGeek"
Hello JustGeek
[root@server ~]#

Printing Variables.

it’s not just for the print text there are many things you can do using echo command. Example you can print Variables.

[root@server ~]# IamVariable="This is JustGeek"
[root@server ~]# echo $IamVariable
This is JustGeek
[root@server ~]#

You can also run a command through echo command and print it’s output to the screen.

[root@server ~]# echo "This server is online from $(uptime | awk '{print $3,$4}')"
This server is online from 77 days,

Formatting using Echo Command.

To use formatting functions with echo -e. It enables interpretation of backslash escapes.
eg:- “\n” parameter will print on the new line. As shown below.

[root@server ~]# echo -e "First Line\nSecond Line"
First Line
Second Line
[root@server ~]#

There are multiple characters which can be used which suits you.

\a: Alert (historically known as BEL). This generates the default alert sound.
\b: Writes a backspace character.
\c: Abandons any further output.
\e: Writes an escape character.
\f: Writes a form feed character.
\n: Writes a new line.
\r: Writes a carriage return.
\t: Writes a horizontal tab.
\v: Writes a vertical tab.
\\: Writes a backslash character.

Leave a Reply

Your email address will not be published. Required fields are marked *