Easy Tutorial
❮ Linux Comm Rpm Linux Filesystem ❯

Shell echo Command

The 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. Displaying Plain 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. Displaying Escaped Characters

echo "\"It is a test\""

The result will be:

"It is a test"

Similarly, the double quotes can also be omitted.

3. Displaying Variables

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

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

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

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

4. Displaying Newline

echo -e "OK! \n" # -e enables interpretation of backslash escapes
echo "It is a test"

The output result:

OK!

It is a test

5. Displaying Without Newline

#!/bin/sh
echo -e "OK! \c" # -e enables interpretation of backslash escapes \c suppresses trailing newline
echo "It is a test"

The output result:

OK! It is a test

6. Displaying Result Directed to a File

echo "It is a test" > myfile

7. Displaying Strings Literally, Without Escaping or Variable Substitution (Using Single Quotes)

echo '$name\"'

The output result:

$name\"

8. Displaying Command Execution Result

echo `date`

Note: Here, backticks ``are used, not single quotes'`.

The result will display the current date:

Thu Jul 24 10:08:46 CST 2014
❮ Linux Comm Rpm Linux Filesystem ❯