In this tutorial, we will learn how to use the echo command in Linux.
You might have heard an echo in science where it is considered as repeated sound. Here, it is totally different. Don’t mess Linux commands with science’s terminologies. Folks don’t get confused with this. The echo command in Linux is one of the basic commands which is used in Linux shells. For example, using echo you can print the path of JAVA_HOME.
If you want such, you have to install Java in the system. Learn How to install Java in Linux.
So let’s discuss the echo command in Linux. Basically it has many uses such as:
- To display text or string which are passes as arguments
- Print the variable with a value
- Only print the value as discussed above
- Display number of files of the same format
- Remove all spaces and so on.
Just look at the uses. Can you imagine, one command has so many uses? Great! Basically, echo is mostly used in UNIX/LINUX operating systems. We will discuss the more examples related to echo command further. Let’s first discuss the syntax of echo.
The syntax of the echo command in Linux is as follows:
echo [option] [string]
In the above example, it will print the text as given. The text to be printed should be in double quotations. Hold on! Let’s clear more about this. If you want to simply print the text, do as stated in the above example. But if you want to include literal quote marks in the output, do the following:
$ echo \'Hello World\'
It will simply print the ‘Hello World‘ with single quotes. You can also use another method to print the text with a single quotation. Use single quote enclosed within double quotes as shown below:
$ echo "'Hello World'"
It will also print the ‘Hello World’ with single quotations. What if, you want to print the text with double quotation. Just do the reverse i.e. enclose double quote within single quote as shown below:
$ echo '"Hello World"'
It will print “Hello World”. I hope this concept is cleared.
Let’s discuss some commonly used options with echo command in Linux.
- –e: Enable the interpretation of backslash escape sequences. A few examples of escape sequences that can be used with -e:
- \a: Alerts with a small sound.
- \n: displays a new line
- \b: Remove the spaces between words
- \t: give a horizontal space
- \r: simulates a carriage return
- \v: Give the vertical space
- -n: to omit new line
Backspace Escape Sequence
This escape sequence simulates the backspace keypress when the echo command in Linux executes. As you can see in the image below, one space is deleted between the words “for” and “Devices”.
$ echo -e "Linux for \bdeivces"
Write a new line
Let’s discuss another escape sequence. What if you want to split the characters of the line or you to print all the characters in a separate line. So for this, we will use \n escape sequence. We will use this with -e option. Let’s have a look at the command below:
$ echo -e "Linux \nfor \ndevices"
The text is split and printed in a new line where we have used ‘\n’ escape sequence.
Add tab space
As \n lets you print the text in a new line, \t inserts a tab between characters. This escape sequence is used with the -e option. Don’t get confused between space and tab space, both are different. Let’s have a look at the command below:
The syntax of this command is as follows:
$ echo -e "Linux \t for \t Devices"
You can see that it has created a tab space or horizontal space between the words.
Vertical space using \v with -e option
This alternative lets you create vertical spaces between the words. As discussed earlier, \t is used to give horizontal tab spaces while \v is utilized to make the vertical tab spaces. Don’t get mistaken while using these escape sequences. To give such vertical space we will use \v with -e option. Let’s have a look at the syntax below:
$ echo -e "Linux \vfor \vDevices"
This really seems interesting. You can make different patterns using such escape sequences with -e option.
Alert with the POP sound using \a
You would remember the pop sound studied in 10th grade? The identification test of Hydrogen gas where you just bring out the candle near the test tube, and it produces a pop? Here, it is different. This escape sequence makes an alert sound i.e. it will produce a small beep sound when executed. For this, we will use \a with -e option. Let’s have a look at the command below:
$ echo -e "Linux \afor Devices"
While executing this command, you will simply hear a small alert sound. It’s really amazing. Do try on your end. It will make you remember the 10th standard chemistry practical!
Print all folders or files using echo command in Linux
Let’s try something new. What if, you want to print all the files and folders, or want to print the files of a particular extension. Yeah! I know you will say that we can use ls command. But Folks, we have to print all the files and folders using echo command in Linux. For this, simply use “*” after an echo. It will list all the directories located in the section. Let’s have a look at the following command:
$ echo *
Here, it would see all the files and directories located on the desktop.
Print files of a particular type using the echo command
Earlier we discussed how to list all the files and folders. What if, you want to print the file of the particular pattern. Do nothing, but just simply mention the file type after “*”. We will use ” *” with echo command in Linux. Be careful when you mention the file type. Let’s have a look at the syntax given below:
$ echo *.txt
In the given example, It will list all the files of .txt extension i.e. text files.
Omit newline using -n option
Earlier we discussed the \n – backslash escape sequence which is used to split the content or we can say that to print the characters separately in the new line. Here, it is different. After the echo command prints the strings, it ends the output with a new line. We have an option to skip the newline at the end of the output.
For this, we will use the -n option. It will omit the new line. Let’s have a look at the syntax below:
$ echo -n "Linux for Devices"
The prompt will be printed on the same line with the content as shown in the above image.
Print Variable Values
As discussed earlier, we can also print the assigned values of the variable. The value must have a value. Earlier, I have discussed how to print the value of JAVA_HOME. In this case, the value to JAVA_HOME is already assigned. You need to use “$” to print the value of the variable. Let’s have a look at the command below:
$ echo The value of x is = $x
In this given example above, it has printed the value of x.
Store data in a file
If you wish to store the output of the echo command in Linux in a file, you can make use of the output redirection operator “>” and specify a name for the output file. Have a look at the example below where I have demonstrated how to store the output of the echo command in Linux.
$ echo "hello world" > hello.txt
$ cat hello.txt
That’s it! The output of the echo command in Linux is now stored in the “hello.txt” file.
In this tutorial, we’ve gone over echo command in Linux. We hope that you now know how to use the command efficiently. If you have any questions, do let us know in the comments section.