Site icon DesignLinux

Echo Command in Linux with Examples

Echo Command in Linux with Examples

In Linux, the echo command is most common and frequently used command. It will print the passed arguments to the standard output. Generally, echo command is used in shell scripts to show a message or output the results of other commands.

echo Command

By default, the echo is built-in in Bash and other shells like Zsh and Ksh. It reacts different in different shell.

In this guide we will cover the Bash built-in version of echo. There is also a standalone /usr/bin/echo utility, but usually, the shell built-in version will take precedence.

The basic syntax for the echo command is as following:

echo [-neE] [ARGUMENTS]

Following are points to be keep in mind while using the echo command:

echo Examples

The following examples show how to use the echo command:

Print a line of text on standard output.

echo Hello, TecNStuff!
Hello, TecNStuff!

Display a line of text containing a double quote.

To print a double quote, enclose it within single quotes or escape it with the backslash character.

echo 'Hello "TecNStuff"'
echo "Hello \"TecNStuff\""
Hello "TecNStuff"

Display a line of text containing a single quote.

You should enclose the string using double quotes use the ANSI-C Quoting, to print the single quotes

echo "I'm doing good."
echo $'I\'m doing good.'
I'm doing good.

Display a message containing special characters.

Use the -e option to enable the interpretation of the escape characters.

echo -e "If it doesn't challenge, it wont change you.\n\t- Kunj"
If it doesn't challange, it wont change you.
    - Kunj

Pattern matching characters.

You can use the echo command with pattern matching characters, like the wildcard characters. For instance, below command will show the names of all the .txt files in the current working directory.

echo The TXT files are: *.txt

The PHP files are: numbers.txt names.txt clac.txt

Output to a file

You can redirect the output to the file using the >, >> operators, instead of displaying the output on the screen.

echo -e 'In a gentle way, you can shake the world. \nGandhiji' >> /tmp/file.txt

It will create a file.txt if it doesn’t exist. If you use the > operator it will be overwrite, while use the >> will append the output to the file.

Use the cat command to view the content of the file:

cat /tmp/file.txt
In a gentle way, you can shake the world. 
Gandhiji

Displaying variables

The echo can be use to display variables. Let’s see an example to print the name of the currently logged in user:

echo $USER
tecnstuff

$USER is a shell variable that holds your username.

Displaying output of a command

Use the $(command) expression to include the command output in the echo’s argument. The following command will display the current date :

echo "The date is: $(date +%D)"
The date is: 10/20/20

Conclusion

You learned how to use the echo command in Linux.

If you have any questions or feedback, feel free to leave a comment.

Exit mobile version