Linux echo Command Tutorial for Beginners (5 Examples)
On this page
Suppose you want to append a hard-coded line of text to a file through the command in Linux, what would you do? An obvious approach would be to open the file in an editor, and then enter the line manually. But think of a case where-in this task needs to be automated. What's your option in that case?
Well, there exists a command - dubbed echo - that you can use in the aforementioned use-case. There are some other situations as well where this tool proves to be helpful. In this tutorial, we will discuss basics of echo as well as the command line options it provides. But before that, it's worth sharing that all commands and instructions mentioned in this tutorial have been tested on Ubuntu 16.04LTS.
Linux echo command
Here's the command's generic syntax:
echo [SHORT-OPTION]... [STRING]...
And here's what the tool's man page says about it:
echo - display a line of text
Echo the STRING(s) to standard output.
The following Q&A-type examples will give you a better idea about how the echo command works.
Q1. How to display a line of text in output using echo?
This is very easy - all you have to do is to pass the line of text as input to the echo command.
Here's an example:
Now, coming to the question that was put up in the beginning of this tutorial, if you want to append a line to an existing file, here's how you can do this using echo:
echo [line-of-text] >> filename
Here's the aforementioned command in action:
Q2. How to disable trailing newline character in echo output?
By default, the echo command output ends with a newline character. However, if you want, you can change this behavior using the -n command line option.
Following screenshot shows the output didn't contain newline when -n was used:
Q3. How echo handles backslash escapes?
When used in default mode, the echo command doesn't treat backslash escapes as special characters, and hence doesn't interpret them in any special way. For example, the following screenshot shows the backspace (\b) character was displayed as it is in the output:
However, if you want, you can change this behavior by using the -e command line option.
So now you can see that the special character was interpreted as backspace, and 'o' was deleted in the output.
Q4. How echo sometimes act as ls command alternative?
You can also use echo in place of ls in certain scenarios. For example, to list all .jpg files in the current directory, you can simply execute the following command:
Q5. How echo helps in other real-world use cases?
The echo command comes in really handy while working with variables (both shell and environment) on the command line. Specifically, you can easily display value of a variable using the tool - all you need to do is to pass the variable name (preceded with $ sign) to the echo command.
Following screenshot shows you a couple of examples:
Clearly, echo is small command line utility that doesn't offer many command line options, but it does what it promises. There isn't exactly a learning curve associated with this tool - just understand and try out whatever we've mentioned here, and you'll be good to go. For more information, head to echo's man page.