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.
Option | Description |
-n | Display the output without a newline after it. This option suppresses the trailing newline. |
-E | Disable the escape characters' interpretation. This is the default option. |
-e | Enable the following escape characters interpretation: |
\\ | Display backslash-escaped characters. |
\a | Display the output with a sound alert. |
\b | Create a backspace character, which is equivalent to using the backspace key. |
\c | Ignore any output after the escape character. |
\e | Create an escape character, which is equivalent to using the Esc key. |
\f | Create a form feed character, which makes the printer automatically go to the beginning of the next page. |
\n | Add a new line in the output. |
\r | Perform a carriage return. |
\v | Create vertical tab spaces. |
\t | Create horizontal tab spaces. |
\NNN | Octal byte value of NNN. |
\xHH | 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:
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."
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:
echo $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)”
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
orecho > 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.
Comments