The shell commands have always been a crucial tool in Linux. So learning about them gives a user fine-grained control over the Linux machine. Such a command of Linux bash shell is echo command. However, the echo command seems to be a pretty straightforward and easy one. It has a unique job that cannot be done with other commands, especially while writing a bash script. The echo command in Linux is mainly used for printing text in the console. It can show messages for the user while a bash script is executing.
What is The Use of echo Command in Linux?
Let’s dive deeper into the echo command and see how we can use it to display text in the console and format the output in different ways. We will also see how to use the echo command beyond just printing text.
1. Printing Text in the Console
If we want to show some text in the console, we can use the echo command in three ways. The first one is like this,
echo Hello everyone
You can also execute the command in this way
echo 'Hello everyone'
Or the third option you have is,
echo "Hello everyone"
Although all three are good for simple use, it is recommended to use the double quote to avoid any kind of unexpected behavior.
2. Omit Newline in Output
If you are an attentive learner, you may have already noticed that the echo command prints a newline at the end of the text. To better understand the effect, we can use the echo command without any text.
Now, if we do not want this newline after our output, we can use the echo command like this.
echo -n "ubuntupit"
Here you can see that the cursor has not gone to the next line for typing the next command.
Before discussing the technique of other types of formatting, we have first to understand another concept. Echo command formats its output with a backslash and a letter immediately after that. This is called backslash escape.
By default, this feature is turned off. To use this, we have to first add -e
flag (Careful! It is lowercase e, not uppercase) with the command so that it interprets the backslash escapes. The commands we are going to use now will use -e
flag a lot.
3. Alert Sound
If you are using the computer for a while, you must have heard the alert sound. With the echo command, You can make your text to be printed with that alert sound of the Linux system to notify the user of your script about something. Type and execute the command given below to hear the effect. Make sure your soundbox is working properly.
echo -e "ubuntupit\a"
4. Backspace
Another escape character is \b
. It takes your cursor one character back. Writing anything after this will start printing from the new cursor position. Let’s see an example
echo -e "ubuntu\b\b\bOS"
Here three \b
has taken the cursor behind the letter n, and when OS is printed, it starts printing from that position.
5. Stop Printing
We have already seen \a
and \b
. The next is as expected; \c
. What does it do? See the example below,
echo -e "ubuntu\cpit"
Maybe you have understood what it does. This \c makes the echo command ignore any character after it.
6. Delete One Letter
Another escape character of echo command in Linux is \e
. It is something like the delete key on the keyboard. It removes one visible character after this. If there is any space before that visible one, \e
also delete that. Run the command below to understand its use.
echo -e "hello\e world"
7. Add a Newline
To add a new line to our text, we can use \n
character. See the example below
echo -e "Linux\noperating\nsystem"
8. Carriage Return
Carriage return takes the cursor to the beginning of the line. So the next character starts printing from the beginning of the line. We can demonstrate it with the example below
echo -e "Linux\rMint"
9. Add Tab
You can print a horizontal tab using \t
. For example
echo -e "hello\tworld\tprogram"
For the vertical tab, we use \v
. You can see it by running the code
echo -e "hello\vworld\vprogram"
10. Embedding ASCII Code
With the echo command in Linux, we can also print characters or use control code using their ASCII values. But we must use the octal or hexadecimal values of the ASCII codes with a backslash. Let us see the ASCII code in action with examples.
In the previous example, we have used the vertical tab using \v
 . The ASCII control code for the vertical tab is 11 in decimal. Which is 13 in octal and B in hexadecimal. Here is an important thing to remember.
If we use the octal number for ASCII code, we have to add a 0
before that, and if we use the hexadecimal number for that, we must add an x
before the number. Run the commands given below to understand it properly.
echo -e "Hello\013world" echo -e "Hello\xbworld"
You can see that we have used vertical tab using octal and hexadecimal ASCII code. For hexadecimal numbers, it does not matter whether you use a block letter (like B) or a small letter (like b); both are fine.
We can also print any visible character in this way. For example,
echo -e "\0141" echo -e "\x61"
Here we have used ASCII code for letter a. You can use any ASCII code in this way.
11. Styling with ANSI Escape Codes
This is really interesting. We can style our codes using ANSI escape sequences and beautify our output. Though all ANSI escape sequences may not work on all systems, yet this is our best choice while beautifying our console output. To enable ANSI escape, we must use ASCII code for escape, which is 33
in octal and 1B
in hexadecimal. Here is an example.
echo -e "\033[7mhello" echo -e "\x1b[7mhello"
This ANSI escape code reverses the background and font color of the text. There is a lot of ANSI escape codes. You can use any of them as per your need.
12. Trick with Backslash
Suppose we want to print the line “It is better to use the double quote, ie.” with echo command in linux. For this, we may run the command below.
echo "It is better to use double quote ie. " with echo command in linux"
Now we are stuck. Hit control + C to get free. But what happened? Actually, the echo command became confused with the "
in the middle of the line and started demanding more input. We can easily avoid this situation with a simple thing – Backslash. Run the command below and see the magic.
echo "It is better to use double quote ie. \" with echo command in linux"
Now everything is fine. But there is more.
Suppose we want to print the line “We use \n for newline” with a bell sound. It seems simple, isn’t it? Why not run the command and see What happens.
echo -e "\aWe use \n for newline"
Oops. We can hear the bell but cannot print \n
. You may think of dropping the -e
flag.
echo "\aWe use \n for newline"
Mission failed! Now we can definitely see \n
but cannot hear the sound. And as a bonus, have a \a
with our text. So what is the solution? Backslash.
echo -e "\aWe use \\\n for newline"
We have finally got our desired output.
13. Print Value of a Variable
If a variable is declared, the value of that variable can be seen with the echo command. An example is given below.
x="ubuntupit" echo $x
We can also use the echo command for viewing the value of the environment variables. For example
echo $HOME
14. Using echo Command with Other Commands
We can use the echo command with other Linux commands. When writing a bash script, it is often required to use the output from one command with some text. Here is an example of using the echo command along with the date command.
echo "Today is $(date +%A). Thank you"
15. Listing Directories and Files
One interesting use of the echo command is to list directories and files. We can list all the files and directories within a directory using the echo command like this.
echo *
We can also search files or folders with this. For example,
echo D* echo *.txt
in the first command, we searched for all files and directories starting with the letter D. With the second command, and we searched for all files and directories ending with .txt
extension.
16. Writing File
We can create a file and write something in it with the echo command. For this, we redirect the text into a file. If the file does not exist, it is created. Otherwise, it may overwrite the content of the file or add new text to the file. Let us understand this with example.
If we want to create a file, we use the > operator with the echo command. Copy and execute the command below to see the effect on your machine.
echo "ubuntupit" > myfile.txt
This command will create a file named myfile.txt inside your present working directory. Inside the file, the text ubuntupit will be written. We can see it with the cat command.
cat myfile.txt
Now, If we use the >> operator, it will add a line to our file. Run the command below.
echo "website" >> myfile.txt cat myfile.txt
In this way, we can add more and more lines to our file. This becomes extremely helpful when writing a bash script that will automatically create and update log files.
What will happen if we use the > operator in our existing file? Let us see. Type and execute the command given below.
echo "linux" > myfile.txt cat myfile.txt
We can see that using > operator clears the whole file and starts writing from the top.
Summary
The echo command in Linux seems to be a simple one at first glance, but we have already shown you how to use this command in different ways. We can print text, format the output in various manners, use it with other commands to write interactive scripts, know the values of variables and write to files. This echo command is a useful tool.
I hope you can now use the echo command efficiently from now on. If you are planning to write a bash script, you may want to bookmark this page to create a better script with proper outputs. You can also express your thoughts on the echo command here in the comment section.