English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Shell echo Command

The echo command in Shell is similar to the echo command in PHP, both of which are used for string output. Command format:

echo string

You can use echo to achieve more complex output format control.

1.Display ordinary strings:

echo "It is a test"

The double quotes can be omitted here. The following command has the same effect as the above example:

echo It is a test

2.Display escape characters

echo "\"It is a test\""

The result will be:

"It is a test"

Similarly, double quotes can also be omitted

3.Display the variable

The read command reads a line from standard input and assigns the value of each field of the input line to the shell variable

#!/bin/sh
read name 
echo "$name It is a test"

The above code is saved as test.sh, the variable name receives the standard input variable, and the result will be:

[root@www ~]# sh test.sh
OK                     # Standard input
OK It is a test          # Output

4.Display with newline

echo -e "OK! \n" # -e Enable escaping
echo "It is a test"

Output result:

OK!
It is a test

5.Display without newline

#!/bin/sh
echo -e "OK! \c" # -e Enable escaping \c No newline
echo "It is a test"

Output result:

OK! It is a test

6.Display the result redirected to a file

echo "It is a test" > myfile

7.Output the string as is, without escaping or retrieving variables (use single quotes)

echo '$name\"'

Output result:

$name\"

8.Display the command execution result

echo `date`

Note: Here we use backticks `, instead of single quotes '.

The result will display the current date

Thu Jul 24 10:08:46 CST 2018