Shell echo command


Shell’s echo command is similar to PHP’s echo command, both 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 here can be completely omitted. 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, the double quotes can also be omitted

3. Display variables

read command reads a line from the standard input, And assign 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, name receives the standard input variable, the result will be:

[root@www ~]# sh test.sh
OK                     #标准输入
OK It is a test        #输出

4. Display Line break

echo -e "OK! \n" # -e 开启转义
echo "It it a test"

Output result:

OK!

It it a test

5. Display without line break

#!/bin/sh
echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"

Output result:

OK! It is a test

6. Display result directed to file

echo "It is a test" > myfile

7. Output the string as it is without escaping or taking variables (using single quotes)

echo '$name\"'

Output results:

$name\"

8. Display the command execution results

  echo `date`

The results will show the current date

Thu Jul 24 10:08:46 CST 2014