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.
- 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.
|Display the output without a newline after it. This option suppresses the trailing newline.|
|Disable the escape characters' interpretation. This is the default option.|
|Enable the following escape characters interpretation:|
|Display backslash-escaped characters.|
|Display the output with a sound alert.|
|Create a backspace character, which is equivalent to using the backspace key.|
|Ignore any output after the escape character.|
|Create an escape character, which is equivalent to using the Esc key.|
|Create a form feed character, which makes the printer automatically go to the beginning of the next page.|
|Add a new line in the output.|
|Perform a carriage return.|
|Create vertical tab spaces.|
|Create horizontal tab spaces.|
|Octal byte value of NNN.|
|Hexadecimal 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:
To display a simple line of text on the standard Linux output without any special characters or options, type:
echo "Hi, Linux user."
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\””
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.”
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.’
Use the \n escape character to move the output to a new line:
echo -e ‘Hello, \nWorld! \nHow \nare \nyou.’
To add a horizontal tab before a word, use the \t escape character:
echo -e ‘Hello, \tWorld! \tHow \tare \tyou.’
To add a vertical tab before a word, use the \v escape character:
echo -e ‘Hello, \vWorld! \vHow \vare \vyou.’
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.’
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.”
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:
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)”
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.logor
echo > error.log
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.