As an admin, you may regularly run the echo command in Bash when managing a Linux system. But is printing texts all the echo command can offer?
In this tutorial, you will take a closer look at the echo command and learn some of its most useful practical examples.
Ready? Stay tuned and reshape your idea of the echo command!
Prerequisite
This tutorial comprises hands-on demonstrations. To follow along, you only need an Ubuntu machine. This tutorial uses Ubuntu 20.04, but other Linux distributions will also work.
Printing Strings with the echo Command in Bash Shell
When you think about the echo command, the first thing that comes to mind is printing string values to the console. Yes, the echo command returns strings directly from a command or script.
But later in this tutorial, you will see the echo command in a different view. For now, see how this command prints out strings to the console.
The syntax for the echo command is as follows where:
- string – the text to print.
- options – the optional arguments you can specify to modify the output.
echo string options
Run the below echo command without added options to print a simple text to the console. This command passes the string as an argument to the echo command.
echo "Echo command bash"
Suppressing Trailing Newlines in Strings
The echo command adds a newline character at the end of each string by default. As a result, when you print multiple strings, perhaps connecting one to another, each string prints on a new line.
The -n option suppresses trailing newlines telling the echo command to print multiple strings on a single line. This option can be helpful, for example, when printing progress bars to the console.
Run the below command to print Progress: and 50%.
echo -n "Progress: " && echo "50%"
Below, you can see newlines separate the two strings.
Now, run the following command, appending the -n option, to print the strings Progress: and 50%.
echo -n "Progress: " && echo "50%"
As you can see below, both strings are now in a single line since you used the -n option to suppress the newlines.
Printing Variable Values
When writing a script, you typically temporarily store a value to a variable, which you can print with the echo command. This function can be helpful when checking the value of a variable to debug a shell script.
Run the commands below to set the number variable’s value to 10 and print (echo) the value to the console.
# Stores the value of 10 to the number variable
number=10
# Prints the current value stored in the number variable
echo The number is $number
Interpreting Escape Sequences
An escape sequence is a series of characters representing a special character, and you can use the -e option to interpret escape sequences.
For example, the \t escape sequence represents a tab character. If you use the -e option and print \t to the console, \t will be interpreted as a tab character instead of being printed literally.
Some of the most common escape sequences are as follows:
Escape Sequence | Function |
\b | backspace |
\t | tab |
\n | new line |
\\ | backslash |
\$ | dollar sign |
Run the following command to print Hello\tWorld to the console.
echo "Hello\tWorld"
You can see below that the echo command printed the string Hello\tWorld as is.
Now, run the below command to print Hello\tWorld, appending the -e option to interpret \t as a tab character in between.
echo -e "Hello\tWorld"
Notice below the huge gap between the word Hello and World.
Inserting Strings to a New Line
Printing a long single line of string makes a user struggle to read its entirety. Luckily, the \n option lets you add a new line character in the middle of a string. This option is often used when printing multiple lines of text to the console, which gives clarity to the actual text.
Run the below command to print the string Echo command bash\nAnother line, where the \n option breaks the string into two lines.
echo -e "Echo command bash\nAnother line"
Below, you can see the \n option put the string Another line to a new line.
Inserting Vertical Tabs Between Strings
You have seen how the \t escape sequence puts a horizontal tab character between strings. But how about a vertical tab? The \v option lets you separate strings in vertical tabs, which is perfect for making text banners.
This option is often used when printing multiple columns of text to the console, with each column being on a new line with a new tab space at the beginning.
Run the below command to print the text Echo command bash demo, but with vertical tabs (\v) between each word.
echo -e "Echo \vcommand \vbash \vdemo
Printing File Names Based on Extension
If you are looking for a way to find files with a specific file type, this feature of the echo command works the same way. The echo command supports listing files in a directory with a particular extension with the wildcard character (*).
1. Run the following command to print all files ending with the .txt extension in the current directory.
echo *.txt
2. Next, run the below command to print all files ending with the .deb file extension in the current directory.
echo *.deb
3. Lastly, run the below command to print the files with the .txt extension in the ~/Downloads directory. You can replace ~/Downloads/ with your file’s actual directory path.
echo ~/Downloads/*.txt
You can see below that the echo command supports specifying a directory path where your files are located.
Writing Output to File
When your command returns a massive output, you can barely read everything on the console. Writing the output to a file instead is a great idea. But how? Besides printing texts to the console, the echo command also supports writing outputs directly to a file.
Run the below command, which does not provide output since the output is written (>) to a file called echo-bash.txt instead.
Note that if the echo-bash.txt file already exists, it will be overwritten with the new content.
echo "echo command bash" > echo-bash.txt
Now, run the following cat command to view the contents of the echo-bash.txt file.
cat echo-bash.txt
As you can see below, the echo command output is written to the echo-bash.txt file.
Appending Output to Existing Files
As previously mentioned, the > operator overwrites an existing file with new content. But what if you wish to append the output to an existing file instead of overwriting it?
Instead of a single > operator, make it two >>. This feature helps keep the existing content and append new entries to the end of the file.
Run the below command to append (>>) new texts (Appending text) to the existing echo-bash.txt file.
echo "Appending text" >> echo-bash.txt
Now, run the following cat command to view the contents of the echo-bash.txt file.
cat echo-bash.txt
As you can see below, the echo command output is appended to the file without overwriting anything.
Conclusion
The echo command in the bash shell is a simple but powerful tool that can be used in many ways. And in this tutorial, you have learned how to maximize the echo command’s potential with some of its most popular options.
Printing texts to the console is not just ‘printing texts’ anymore. You have seen how to write output to a file, append outputs, and change how each printed texts look.
Why not pick some ways you can use the echo command and write them on your scripts? See what creative ways you can come up with!