“echo” command in Unix often used in the shell script for display/print message on the terminal.
There are two uses of “echo” command.
- “echo” is used for display/print message on terminal.
- “echo” is also used for evaluate shell variable.
“echo” is used for display/print message on terminal
To print the message on terminal press echo command on terminal and message to print place within “ “ double quote as shown below.
1 | $ echo “ Enter any number = ” [press enter] |
output
1 | Enter any number = [output ] |
Escape sequence: “echo” command interprets certain string as a escape sequence.
The escape sequence is generally (not necessarily ) a two character string which starts with a backslash ( \ ).
For example: “ \c “ is a one escape sequence and if we place “ \c ” escape sequence at the end of the line/string then this escape sequence place the cursor on the same line that displays the output.
1 | $ echo “ Enter any number : \c” [press enter] |
output
1 | Enter any number : $_ [output and cursor on the same line] |
Note: echo escape sequence are the feature of VSBD system. Linux Bash shell, the standard shell used in Linux interprets escape sequence if echo command is used with “–e” option
1 | $ echo -e “ Enter any number : \c” [press enter] |
1 | Enter any number : $_ [output and cursor on the same line] |
Other Escape sequences:
[table id=4 /]
“echo” is also used to evaluate shell variable.
Assign value to variable : we can assign a value to variable on terminal as
1 | $ x=5 [ press enter] |
output
1 | $_ |
Note: When we assign a value to a variable must remember there is no space either side of = sign.
Evaluate the value of a variable: to evaluate the value of variable write “echo” then place $ sign before variable as shown below:
1 | $ echo $x [ press enter] |
output
1 | 5 [ value of x is 5 print on terminal ] |