Echo Command in Linux

Last updated: May 2, 2022

The Linux operating system is widely used to host applications on servers, and system administrators use various shell scripts, batch files, and variables to automate tasks. The variables and script output may not be understandable to the users. In such cases, you can use the echo command to display string or text on standard output.

In this tutorial, you will learn about the echo command in Linux using practical examples.

Prerequisites

  • Linux operating system access.
  • Familiarity with Linux command-line interface.
  • Willingness to learn new Linux commands with examples.

Linux Echo Command

Echo is one of the most commonly used Linux commands that is used to print the specified argument on the screen and print variable values. The echo command in Linux is a shell built-in command, which is available by default.

The most common use of the echo command is to output the text or strings that you pass as an argument to this command. The echo command arguments include static text, shell variables, environment variables, file names, and directories. The echo command works on all major Linux distributions, including Red Hat, Debian, Ubuntu, and Fedora.

Echo Command Syntax

The basic syntax of the echo command is:

echo [option] [string] [variable]

The most commonly used syntax of this command is to enter the string or a line of text after the echo command:

echo Hello, Linux user!

The standard output of the above command will be the specified string:

Hello, Linux user!

Echo Command Options

The echo command options enable you to modify the standard output per your requirements.

OptionDescription
-nDisplay the output without a newline after it. This option suppresses the trailing newline.
-EDisable the escape characters' interpretation. This is the default option.
-eEnable the following escape characters interpretation:
\\Display backslash-escaped characters.
\aDisplay the output with a sound alert.
\bCreate a backspace character, which is equivalent to using the backspace key.
\cIgnore any output after the escape character.
\eCreate an escape character, which is equivalent to using the Esc key.
\fCreate a form feed character, which makes the printer automatically go to the beginning of the next page.
\nAdd a new line in the output.
\rPerform a carriage return.
\vCreate vertical tab spaces.
\tCreate horizontal tab spaces.
\NNNOctal byte value of NNN.
\xHHHexadecimal byte value of HH.

Echo Command Examples

The echo command in Linux provides various options that you can use to modify or format your output as per your requirements. The following echo examples describe some of the common use cases of this command:

Display Text

To display a simple line of text on the standard Linux output without any special characters or options, type:

echo "Hi, Linux user."
Display line of text

For displaying a double quotation mark for a particular word, then use either a single quote for the entire string or use a backslash character for escape:

echo ‘Hello, “World!”’
echo “Hello \”World\””
 display a double quotation mark

For displaying a single quote mark or apostrophe, put the entire string in the double quotation marks:

echo “I’ll go to the office tomorrow.”
display a single quote mark or apostrophe

Use Escape Character with Echo

Use the -e option with the echo command to use escape characters. These special characters help you customize the echo command output.

For example, the \c escape character shortens the output string by not printing the output followed by the escape character. To avoid printing How are you in the following command output, use the echo -e option with /c escape character:

echo -e ‘Hello, World! \c How are you.’
option -e - using \c escape character

Use the \n escape character to move the output to a new line:

echo -e ‘Hello, \nWorld! \nHow \nare \nyou.’
option -e - using \n escape character

To add a horizontal tab before a word, use the \t escape character:

echo -e ‘Hello, \tWorld! \tHow \tare \tyou.’
option -e - using \t escape character

To add a vertical tab before a word, use the \v escape character:

echo -e ‘Hello, \vWorld! \vHow \vare \vyou.’
option -e - using \v escape character

To change the color of the text, you can use the ANSI escape sequence:

echo -e ‘\033[1;37mHello’
echo -e ‘\033[0;30mWorld!’
echo -e ‘\033[0;31mHow’
echo -e ‘\033[0;32mare’
echo -e ‘\033[0;33myou.’
using echo change the color of the text

You can also use echo -e to use multiple escape characters, such as /n for a new line and /t for a horizontal tab in a single command:

echo -e “Life is short. /n/t- A wise man.”
option -e - using multiple escape characters

You can also insert a special character after the new line.

Print Variable Values

For displaying environment variable and shell variable values as the echo command output, use the following echo command:

echo $PWD
print the variable value of $PWD

Print Command Outputs

Using the echo command you can print the result of other commands. You must specify the command in $(command) format, for example, the $(ls) command will display the existing content of the current directory.

To display the output of the other commands along with the specified text string, you can use the following command:

echo -e “File system information of the users.txt file: \n $(stat users.txt)”
display other commands outputs with a string

Empty File

You may use the echo command to empty file (clear the file contents) in Linux. You can pass an empty string or redirect to the file as follows:

echo "" > error.log
or
echo > error.log

Conclusion

In this tutorial, we learned how to use the Linux echo command to print a specific string on the standard Linux output. This command is also used to print environment variable values and command output. The echo command also supports various escape characters that you can use to format the output as per your requirements.

About The Author

Subhash Chandra

Subhash Chandra

Subhash Chandra is a professional writer and an Oracle-certified Database Administrator with over 15 years of writing experience. He has a passion for technology and loves to write how-to articles for Linux, Windows, Mac OS, Networking, Telecom, and Database. In his spare time, he enjoys swimming and playing table tennis.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Comments Off on How to Articles