Learn all about echo command
echo - display a line of text
It is one of the way to display message and convey information to user which can be used in bash scripts.
root:~# man echo
1.1 Default Output
By Default, echo adds new line to the message and prints it on the terminal.
root:~# echo "Hello World !" Hello World ! root:~# root:~# echo 'Hello World !' Hello World ! root:~#
root:~# echo -n "Hello World !" Hello World !root:~# root:~# root:~# root:~# echo -n 'Hello World !' Hello World !root:~#
2.1 Difference between Single(') and Double Quote(")
when single quote is given, echo command takes message as literal string but when double quote is given, variable or command substitution can happen.
root:~# echo "Current Directory [$PWD]" Current Directory [/root] root:~# echo 'Current Directory [$PWD]' Current Directory [$PWD] root:~# root:~# echo "Current Directory [`echo Hello $PWD`]" Current Directory [Hello /root] root:~# echo 'Current Directory [`echo Hello $PWD`]' Current Directory [`echo Hello $PWD`] root:~# NAME=parth root:~# echo "Hello $NAME" Hello parth root:~# echo 'Hello $NAME' Hello $NAME root:~#
Sequence | Name |
---|---|
\\ | backslash |
\a | alert (BEL) |
\b | backspace |
\c | produce no further output |
\e | escape |
\f | form feed |
\n | new line |
\r | carriage return |
\t | horizontal tab |
\v | vertical tab |
\0NNN | byte with octal value NNN (1 to 3 digits) |
\xHH | byte with hexadecimal value HH (1 to 2 digits) |
root:~# echo "Hello\n\tWorld" Hello\n\tWorld root:~# echo -e "Hello\n\tWorld" Hello World root:~# echo 'This line is not completely \cprinted.' This line is not completely \cprinted. root:~# echo -e 'This line is not completely \cprinted.' This line is not completely root:~# root:~# echo -e "\vCandidInformation \vis \vall \vabout \vLinux" CandidInformation is all about Linux root:~#
Characters | Description |
---|---|
* | Anything |
[a-z] | Any single character between a to z |
[A-Z] | Any single character between A to Z |
[0-9] | Any single digit between 0 to 9 |
? | Any single character |
ls
command. It can be handy in recovery mode.
root:~# echo "/tmp/*" /tmp/* root:~# echo /tmp/* /tmp/1 /tmp/1231131 /tmp/AAA /tmp/BcD /tmp/b /tmp/c /tmp/ffprobe_output /tmp/file1.txt /tmp/file2.txt /tmp/file3.txt /tmp/file4.txt /tmp/hsperfdata_root /tmp/multi-lang-movie.mp4
root:~# echo /tmp/*.txt /tmp/file1.txt /tmp/file2.txt /tmp/file3.txt /tmp/file4.txt root:~# echo /tmp/*.txt1 /tmp/*.txt1As shown, no files are present with txt1 extension so echo will print string as it is.
root@:~# echo /tmp/? /tmp/1 /tmp/b /tmp/c
root@:~# ls /tmp/[0-9]* /tmp/1 /tmp/1231131
root@:~# echo /tmp/[a-z]* /tmp/b /tmp/c /tmp/ffprobe_output /tmp/file1.txt /tmp/file2.txt /tmp/file3.txt /tmp/file4.txt /tmp/hsperfdata_root /tmp/multi-lang-movie.mp4 root@:~# echo /tmp/[A-Z]* /tmp/AAA /tmp/BcD root@:~# echo /tmp/[a-zA-Z]* /tmp/AAA /tmp/BcD /tmp/b /tmp/c /tmp/ffprobe_output /tmp/file1.txt /tmp/file2.txt /tmp/file3.txt /tmp/file4.txt /tmp/hsperfdata_root /tmp/multi-lang-movie.mp4
4.1 Colored Output
Color | ForeGround Normal Code | ForeGround Bright Code | Background Code |
---|---|---|---|
Black | 0;30 | N.A. | 40 |
Red | 0;31 | 1;31 | 41 |
Green | 0;32 | 1;32 | 42 |
Yellow | 0;33 | 1;33 | 43 |
Blue | 0;34 | 1;34 | 44 |
magenta | 0;35 | 1;35 | 45 |
Cyan | 0;36 | 1;36 | 46 |
Gray | 1;30 | 0;37 | 47 |
White | 1;37 | N.A. | 0 |
N.A.
- Not Applicable
\ESC[
begins the escape sequence. Based on ASCII table value should be \033
. The semicolon-separated numbers COLOR1
and COLOR2
specify a foreground and a background color, according to the table above. The order of the numbers does not matter, since the foreground and background numbers fall in non-overlapping ranges.Next, the m
terminates the escape sequence, and the text begins immediately after that.
\033[00m
; otherwise output will change your terminal background color too. \033[00m
means, we are terminating with no color !
root@:~# echo -e "\033[0;31;44mThis prints text in red color with blue background\033[00m"This prints text in red color with blue background
root@:~# echo -e "\033[1;33;42mThis prints text in yellow color with green background\033[00m"This prints text in yellow color with green background