The echo command is useful for conveying a message/information to user. This command is more useful when you are writing shell scripts then normal system administration use. In this post we will see some basic examples as well as advanced stuff to get most of it. We have other command printf which is similar to echo command which is having more capabilities like padding, formatting etc. We will see that in another post.

Syntax of echo command in Linux

echo [options] message

echo command basics.

Example 1a: Print something using echo command.

root@linuxnix: ~# echo How are you man?
How are you man?

Example 1b: If you observe the above command we did not use any quotes around the sentence which we are printing. This is ok, but not a best practice. We suggest you to use either single quotes or double quotes.

root@linuxnix: ~# echo 'How are you man?'
How are you man?
root@linuxnix: ~# echo "How are you man?"
How are you man?
root@linuxnix: ~#

Example 1c: Ok, you may get a question in your mind now. What is the difference between single(”) and double (”) quotes. The single quotes are useful for literal/plain printing where as double quotes are useful for variable substitution and command substitution. We will learn this with some example. I will create a variable which stores some information it. By using double quotes, we can retrive that information.

root@linuxnix: ~# NAME1=surendra
root@linuxnix: ~# echo 'How are you $NAME1'
How are you $NAME1
root@linuxnix: ~# echo "How are you $NAME1"
How are you surendra

Did you see the difference?

With double quotes we can do variable and command substitution . Let us see command substitution example as well. Suppose you want to print your present working directory with some information, use below command.

root@linuxnix: ~# echo "My working directory is $(pwd)"
My working directory is /home/surendra

Example 2: By default, echo will include a new line at the end of statement when it prints. We can eliminate this if we want by using -n option.

root@linuxnix: ~# echo -n "My working directory is $(pwd)"
My working directory is /home/surendraroot@linuxnix: ~#

Example 3: By default echo will not understand escape characters like new line(\n), tab(\t). If we want to enable this, use -e option along echo command.

root@linuxnix: ~# echo -e "My working directory is:\n $(pwd)"
My working directory is:
/home/surendra

Other supported escape characters are as below.

\" double quote
\NNN character with octal value NNN (1 to 3 digit's)
\\ backslash
\a alert (BEL)
\b backspace(Removes empty space)
\c produce no further output
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\xHH byte with hexadecimal value HH (1 to 2 digit's)

Advanced usage of echo command.

Example 4: The echo command supports wildcard characters. With this support, we can use echo as ls command. This will come handy when ls will not work in recovery mode. The supported wildcard characters are as below.

* -Anything
[a-z] -Any single character between a to z
[0-9] -Any single digit between 0 to 9
[A-Z] -Any single character between A to Z
? -Any single character.

Example 4a: List all files in a given directory.

echo *

Output:

surendra@linuxnix:~/code/sh$ echo *
ade config.csv fpga_build.sh oldtags.txt red startup_script.sh temp wifiactivate.sh xyz

Example 4b: Print only shell script files.

surendra@linuxnix:~/code/sh$ echo *.sh
fpga_build.sh startup_script.sh wifiactivate.sh

Example 4c: List all the files which starts with digit’s.

echo [0-9]*

Output:

surendra@linuxnix:~/code/sh$ touch 99abc.txt
 surendra@linuxnix:~/code/sh$ echo [0-9]*
 99abc.txt

Example 4d: List files whose name is just a character.

surendra@linuxnix:~/code/sh$ touch a 1 3
surendra@linuxnix:~/code/sh$ ls
1 3 99abc.txt a ade config.csv fpga_build.sh oldtags.txt red startup_script.sh temp wifiactivate.sh xyz
surendra@linuxnix:~/code/sh$ echo ?
1 3 a

I will leave [a-z] and [A-Z] for readers to explore.

Example 5: Print colored output using echo. By using escape sequence\NNN we can print some colors to our output. Suppose if we want to print text in green([32m) use below example.

echo -e "Now my color changes to \033[32mGreen"

Output:

Now my colour changes to Green

How about change background color to red and keep the green word color as green it self.

echo -e "Now my color changes to \033[41;32mGreen"

Output:

Now my color changes to Green

But the output will change prompt color to red as well. If you want to change color only to output we can disable coloring using escape sequence the end of print output.

echo -e "Now my color changes to \033[44;32mGreen\033[0m"

Output:

Now my color changes to Green

Note: I am unable to change background color for green word in above examples, but the above command will work.

Know more about bash/echo coloring from here: http://misc.flogisoft.com/bash/tip_colors_and_formatting

Example 6: How about writing entire shell script with just single echo command?

surendra@linuxnix:~/code/sh$ echo -e "\nMy hostname is $HOSTNAME\n#############################\nNow going to print disk stats\n#############################\n$(df -h)\n#########################\nNow printing memory stats\n#########################\n$(free -m)\n###########################\nHope you enjoyed this post,\nRegister for e-mail updates\n###########################\n"

Output:

My hostname is linuxnix
#############################
Now going to print disk stats
#############################
Filesystem Size Used Avail Use% Mounted on
udev 1.9G 0 1.9G 0% /dev
tmpfs 388M 12M 377M 3% /run
/dev/sda9 54G 48G 3.3G 94% /
tmpfs 1.9G 190M 1.8G 10% /dev/shm
tmpfs 5.0M 4.0K 5.0M 1% /run/lock
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
tmpfs 388M 0 388M 0% /run/user/0
tmpfs 388M 68K 388M 1% /run/user/1000
#########################
Now printing memory stats
#########################
total used free shared buffers cached
Mem: 3877 2719 1157 263 47 444
-/+ buffers/cache: 2227 1649
Swap: 4020 2507 1513
###########################
Hope you enjoyed this post,
Register for e-mail updates
###########################

Just see the beauty of echo command. Do you have some hacks with you? Post it in comments or send it to surendra@linuxnix.com. I will update this post accordingly.

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.