The Echo Command
In this article, we're going to look at the echo
command, which is useful for showing text on the terminal, as well as the contents of variables. Let's get started!
Basic usage of the echo command
Basic usage of the echo
command is very simple. For example:
echo "Hello World"
The output will be what you expect, it will echo Hello World
onto the screen. You can also use echo
to view the contents of a variable:
msg="Hello World" echo $msg
This also works for built-in shell variables as well:
echo $HOME
Additional Examples
As with most Linux commands, there's definitely more that we can do with echo
;than what we've seen so far.
Audible Alerts
You can also sound an audible alert with echo
as well:
echo -e "\aHello World"
The -e
option allows you to change the format of the output while using echo
.
echo -e "This is a\bLinux server."
The example used \b
within the command, which actually lets you call backspace, which gives you the same behavior as actually pressing backspace. In the above example, the letter "a" will not print, because \b
backspaces that.
Truncating
The ability to truncate, means you can remove something from the output. For example:
echo -e "This is a Linux\c server."
The output of the above command will actually truncate the word right after \c
, which means we'll see the following output:
This is a Linux
Adding a new line
To force a new line to be created:
echo -e "This is a Linux\n server"
The output will end up becoming:
This is a Linux server.
Adding a tab character
To add a tab character to the output:
echo -e "This is a\t Linux\t server."
This will produce the following output:
This is a Linux server.
Redirecting output to a text file
Rather than showing the output on the terminal, we can instruct echo to instead send its output to a text file.
echo "Logfile started: $(date +'%D %T'$" > log.txt
Closing
The basics of the echo
command were covered in this article. Of course, there's more options where that came from - but this should be more than enough to get you started!
You can watch the tutorial here: