Shell Command Echo

Echo in shell is similar to the echo in PHP which all output string.The syntax for echo is:

echo string

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

1. Display ordinary string:

echo "It is a test"

The double quotes here can be 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\""

output:

"It is a test"

Similarly, double quotes can also be omitted.

3. Display variables

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

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

Save the above code to the file “test.sh”,and the name receives the variable from the standard input and output will be

[[email protected] ~]# sh test.sh
OK                     #standrad input
OK It is a test        #output

4. Display line breaks

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

output:

OK!

It is a test

5. Display no wrap

#!/bin/sh
echo -e "OK! \c" # -e turn on escaping \c no wrap
echo "It is a test"

output:

OK! It is a test

6. Display results to a file directly

echo "It is a test" > myfile

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

echo '$name\"'

output:

$name\"

8. Display command execution results

echo `date`

Notice:Back quotes(`) are used here, not single quotes(‘).

The result will show the current date

Mon Apr 20 21:04:54 CST 2020

Add a Comment

Your email address will not be published.