Latest web development tutorials

Shell echo command

Similar to Shell's echo command and PHP's echo command is used to output strings. Format:

echo string

You can use more complex echo control the output format.

1. Display plain string:

echo "It is a test"

Double quotes can be omitted here entirely consistent with the examples above, the effect of the following commands:

echo It is a test

2. The escape character

echo "\"It is a test\""

The result will be:

"It is a test"

Similarly, double quotation marks can be omitted

3. The display variable

read command reads a line from standard input, and to specify the values ​​for each field in the input line to the shell variable

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

Save the above code test.sh, name receive standard input variables, the result will be:

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

4. The Wrap

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

Output:

OK!

It it a test

5. The display does not wrap

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

Output:

OK! It is a test

6. Showing results directed to a file

echo "It is a test" > myfile

7. output as strings, without escaping or take variable (single quotes)

echo '$name\"'

Output:

$name\"

The display command execution results

  echo `date`

The results will show the current date

Thu Jul 24 10:08:46 CST 2014