The echo command is one of the most basic and frequently used commands in Linux. It’s used to print text, variables, and special characters to the standard output, which is usually the terminal.
However, the echo command can do much more than just printing text.
It can also be used to create files and directories, test and debug scripts and commands, format and display messages, and generate output for other commands or programs.
In this article we’ll show you how to use the echo command in Linux with some practical examples. We will cover the basic syntax of the echo command, as well as some of its options and features.
By the end of this article, you will have a better understanding of how to use the echo command in Linux effectively and efficiently.
Table of Contents
What is the echo Command?
The echo command is a way to communicate with your Linux terminal. It allows you to send text, variables, and special characters to the standard output, which is usually the terminal screen.
The echo command is like a messenger that delivers your words to the terminal. It’s a simple but powerful tool that can be used for various purposes, such as:
- Printing text, variables, and special characters to the standard output (e.g., terminal, file, or pipe).
- Creating files and directories with specific content.
- Testing and debugging scripts and commands.
- Formatting and displaying messages, prompts, and menus.
- Generating output for other commands or programs.
How to Use the echo Command
Here are some ways of using the echo command in Linux.
To send a text message to the standard output stream, use the echo command with a string argument enclosed in double quotes.
For example, echo “Hello, world!” will write the string “Hello, world!” followed by a newline character to the standard output.
The command echo “$variable” is used to print the value of a variable to the standard output. A variable is a name that represents some data stored in the memory. The $ symbol is used to access the value of a variable.
For example, $USER is a predefined variable that holds the name of the current user. To print the value of $USER, use echo “$USER”. This will display the current user name on the terminal screen.
Print Special Characters
The command echo “\n” is used to print a special character to the standard output. A special character is a non-printable character that has some effect on the output, such as moving the cursor, clearing the screen, or making a sound.
The \ symbol is used to escape the special character, which means to treat it as a literal character instead of its usual meaning. For example, \n is a special character that represents a newline, which moves the cursor to the next line. To print a newline character, use echo “\n”. This will output a blank line on the terminal screen.
There are many other special characters that can be printed with the echo command, such as:
- \t: A horizontal tab, which moves the cursor to the next tab stop.
- \v: A vertical tab, which moves the cursor down one line and to the same column.
- \a: An alert, which makes a beep sound.
- \b: A backspace, which moves the cursor back one space.
- \r: A carriage return, which moves the cursor to the beginning of the line.
- \c: A control character, which suppresses any further output.
You can see the full list of special characters by running man echo on your terminal.
Writing to a File
One of the lesser known functionalities of the echo command is redirecting the output from terminal into files. You can use the echo “content” > file command to create a file with some content to the standard output.
The > symbol is used to redirect the output of the echo command to a file instead of the terminal screen. The file is the name of the file to be created or overwritten. The content is the text to be written to the file.
For example, echo “This is a test file” > test.txt will create a file named “test.txt” with the text “This is a test file”. If the file already exists, it will be replaced by the new content. If the file does not exist, it will be created.
If you want to append content to the end of an existing file, you can use the >> symbol instead of the > symbol. The >> symbol will redirect the output of the echo command to the end of the file without deleting the previous content.
For example, echo “This is another line” >> test.txt will add the text “This is another line” to the end of the test.txt file, without erasing the text “This is a test file”. This way, you can add more content to a file without losing the original content.
Writing to Both Terminal and File
You might encounter cases where you need to display content in the terminal and store it in a file as well. In this case, you can use the echo “content” | tee file command to create a file with some content to the standard output.
The | symbol is used to pipe the output of the echo command to another command, which is tee in this case. The tee command is used to write the input to both the standard output and a file or files. The file is the path of the file to be created or overwritten. The content is the text to be written to the file and displayed on the terminal screen.
Debug Dynamic Commands
If you are executing dangerous commands in the terminal, then it can go very bad, very quickly.
The rm -rf * command will delete all files and directories in the current working directory recursively and forcefully with a single key press. This is a very dangerous command that can cause irreversible data loss.
By using echo before the command, you can see what files and directories will be deleted without actually deleting them.
The command echo rm -rf * is a way to check how the command rm -rf * will be composed without actually executing it. The echo command will print the command or script to the standard output, which is usually the terminal screen. This can help you avoid mistakes and errors.
For example, if you want to delete all the files in the current directory that start with a certain prefix, you can do so using the following command.
echo rm -rf <prefix>*
In the above example we can see that our current directory has a total of 10 files, but since we used a prefix, only the files which matched the pattern were listed in the output. Once you are sure that this is the command that you want to execute, you can remove the echo from the beginning of the command and execute it as you normally would.
Show Formatted Text
The echo command is often considered a boring and simple command that only prints plain text to the terminal. However, this is not true. You can jazz up your output with a dash of color and style by using some special characters and options.
Let’s see how to make your terminal more colorful and attractive with the echo command with the help of an example:
-eflag in echo is used to enable the interpretation of special characters, such as
\e, which are used to create colors and effects.
\e[1;37;41mpart is used to set the style, color, and background of the text. The
\esymbol indicates the start of an escape sequence, which is a way to control the terminal behavior. The
[symbol indicates the start of a parameter list, which consists of numbers separated by semicolons. The
msymbol indicates the end of the escape sequence. The numbers in the parameter list have different meanings, such as:
1: This means to make the text bold.
37: This means to set the foreground color (the color of the text) to white. The color codes range from 30 to 37 for standard colors, and from 90 to 97 for bright colors.
41: This means to set the background color (the color behind the text) to red. The background color codes range from 40 to 47 for standard colors, and from 100 to 107 for bright colors.
- After this, you can enter the text to be printed with the specified style, color, and background.
\e[0mpart is used to reset the style, color, and background of the text to the default values. The
\esymbol indicates the start of an escape sequence, and the
[0mpart indicates the end of the escape sequence with a parameter of zero, which means to reset all attributes.
Therefore, the command
echo -e "\e[1;37;41mThis is white text on red background\e[0m" will print “This is white text on red background” in bold white letters on a red background, and then reset the terminal settings to normal.
Some more ideas of displaying pretty output in terminal with the echo command are:
- To display a message with an underline, use
\e[4m. For example,
echo -e "\e[4mThis is underlined text\e[0m"will print “This is underlined text” with an underline.
- To display a message with a blinking effect, use
\e[5m. For example,
echo -e "\e[5mThis is blinking text\e[0m"will print “This is blinking text” with a blinking effect.
- To display a message with different colors on each word, use
\e[colormbefore each word. For example, echo -e
"\e[31mThis \e[32mis \e[33ma \e[34mrainbow \e[35mtext\e[0m"will print “This is a rainbow text” with different colors for each word.
You can combine different styles and colors to create more interesting and attractive output in your terminal. However, you should also be aware that not all terminals support these features, and some may display them differently.
You can see the list of available colors and styles by running man console_codes on your terminal.
Execute Other Commands
In a previous section, we discussed how echo can be used to debug commands without executing them. Now let’s see how it can be used to execute commands in bash.
We know that the date command is a Linux utility that displays the current date and time. We can use it along with the echo command to print the output of the date command as a string, and format it accordingly.
echo $(date) will print the current date and time to the standard output. The
$(date) part is an example of command substitution, which is a way to execute a command and replace it with its output. For example, if you run
echo -e "It is \e[1;31m$(date)\e[0m today.", you might see something like this:
We already briefly mentioned in one of the previous sections that the
| operator can be used to redirect output from the first command to the other.
For example, the
echo "Hello" | wc -c command can be used to print the number of characters in “Hello” to the standard output. The “Hello” part is a string argument for the echo command. The
| symbol is used to pipe the output of the echo command to another command, which is
wc -c in this case.
wc -c command is used to count the number of bytes in the input. The echo command will send the string “Hello” to the standard input of the
wc -c command, which will count the number of bytes in “Hello” and print it as a number. Note that the number 6 includes the newline character that the echo command adds by default. If you want to exclude the newline character, you can use the
-n option for the echo command, which will suppress the newline. For example, if you run
echo -n "Hello" | wc -c, you will see something like this:
As you can see, using the echo command with command substitution or pipes can help you generate output for other commands or programs that can process or display it. You can use this technique to create dynamic and interactive output in your terminal.
When Not to Use the echo Command
The echo command is a great tool for printing simple text messages to the terminal, but it has its limitations. Sometimes, you may need to print data that is more complex, structured, binary, or more sensitive than plain text. In these cases, the echo command may not be suitable, and you may want to use other tools that are more specialized and secure.
Here are some examples of such cases, and the tools you can use instead of the echo command:
- If you need to print complex or structured data, such as JSON, XML, or tables, the echo command may not be able to preserve the formatting and indentation of the data. You may want to use tools such as jq, xmllint, or column, which can parse and pretty-print JSON, XML, or tabular data respectively.
- If you need to print binary data, such as images, audio, or video, the echo command may not be able to display them properly in the terminal. You may want to use tools such as cat, hexdump, or base64, which can output binary data as raw bytes, hexadecimal numbers, or base64-encoded strings respectively.
- If you need to print sensitive or confidential information, such as passwords, keys, or tokens, the echo command may not be able to protect them from being exposed or intercepted. You may want to use tools such as gpg or openssl, which can encrypt and decrypt data using various algorithms and keys.
As you can see, there are many alternatives to the echo command that can handle different types of data more effectively and securely. You can choose the best tool for your needs depending on the nature and format of your data.
After Action Report
We hope you enjoyed this article and learned something new about the echo command in Linux. The echo command is a versatile and useful tool that can help you create and communicate with your Linux environment. Feel free to experiment with it and discover its potential!
We would love to hear from you about your own use cases of the echo command. How do you use it in your daily tasks? What are some of the creative and fun ways you have used it? Please share your thoughts and experiences in the comment section below. We appreciate your feedback and suggestions!
We understand that Linux can be intimidating for some people, especially if they are not familiar with the terminal. That’s why we recommend RunCloud as the best platform to manage your Linux servers. RunCloud provides a helpful dashboard that lets you easily deploy and manage your servers, applications, and websites without messing with the terminal.
However, if you want to tinker under the hood, RunCloud also gives you the freedom and flexibility to access and customize your server settings via SSH or SFTP. RunCloud is the perfect solution for both beginners and experts who want to get the most out of their Linux servers.
If you want to experience RunCloud for yourself, you can sign up for RunCloud and enjoy a 14-day, risk-free trial.