Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Shell echo command


May 22, 2021 Linux


Table of contents


Shell echo command

The echo instruction for the shell is similar to the echo instruction for PHP and is used for the output of strings. Command format:

echo string

You can use echo for more complex output format control.

1. Show normal strings:

  echo "It is a test"

The double quotes here can be omitted, and the following command is consistent with the above example:

  echo It is a test

2. Show escape characters

  echo "\"It is a test\""

The result will be:

  "It is a test"

Similarly, double quotes can be omitted

3. Show variables

The read command reads a line from the standard input and assigned 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 .sh, name receives the variables entered by the standard, and the result will be:

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

4. Show line changes

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

Output:

OK!

It it a test

5. Show no line changes

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

Output:

OK! It is a test

6. Display the results directed to the file

  echo "It is a test" > myfile

7. Output string as is, do not escape or take variables (with single quotes)

  echo '$name\"'

Output:

  $name\"

8. Show the results of command execution

  echo `date`

Note: Inverse quotes are ` instead of single ' .

The results show the current date

  Sat Dec 9 14:41:48 CST 2017