- Learn a new language with over 50% off a Rosetta Stone subscription right now
- Buy Microsoft Visio Professional or Microsoft Project Professional 2024 for up to 92% off
- Uncovering the Gaps in Cyberthreat Detection & the Hidden Weaknesses of SIEM
- I went mountain-biking with this $350 DJI camera, and the video results blew me away
- Need a VPN? Buy a 5-year subscription for $35 right now
Many ways to use the echo command on Linux
The echo command (a bash built-in) is one of the very basic commands on Linux. As with ls and pwd, you can’t sit on the command line very long without using it. At the same time, echo has quite a few uses that many of us never take advantage of. So, this post looks into the many ways you can use this command.
What is echo?
Basically, echo is a command that will display any text that you ask it to display. However, when you type “echo hello”, the echo command isn’t only spitting out those five letters, it’s actually sending out six characters – the last one being a linefeed. Let’s look at a couple of commands that make this obvious.
First, using echo with no arguments does this:
$ echo $
Notice that an empty line is displayed before the prompt comes back. If you redirect the command’s output to the od -bc command, it’s easy to confirm that the “invisible” output is simply a linefeed – that n or octal 012 in the output below.
$ echo | od -bc 0000000 012 n 0000001
That n is the reason that the echo command can also be used to add a linefeed to the end of a file that, for some reason, lacks one. Here’s a file that lacks a linefeed followed by the command that adds one.
$ od -bc nolinefeed 0000000 150 145 154 154 157 h e l l o 0000005 $ echo >> nolinefeed $ od -bc nolinefeed 0000000 150 145 154 154 157 012 h e l l o n <== 0000006
od -bc
The od -bc command in the above example displays the content of files in both octal and character format. The output above shows that a linefeed was added to the end of the file. The od -bc command provides a very useful way to examine the contents of a file, especially when using the cat command doesn’t show you everything you need to see.
> vs >>
One important thing to pay attention to when using the echo command is that > and >> work with files differently. The > will overwrite the content of a file, while >> will append to it.
echo -n
The echo -n command will omit the linefeed from its output. This is often used in scripts when prompting the user to provide an answer to some question. This works best when the prompt and the space where the answer will be entered are on the same line. For example, if the user should provide the name of the file to be processed by the script, we’re likely to see lines like these:
echo -n "file name> " read file
Whoever runs the script will be prompted to enter a file name and will type the file name as shown here:
file name> datafile
Emptying files
The echo command can also be used to empty files. While it’s more common to use a command such as cat /dev/null > filename, the command echo -n > filename works as well. Both commands replace the content of a file with … nothing! To turn this command into an alias, use a command like this or add it to your .bashrc file.
$ alias empty='echo -n > '
After setting up the alias, you can just type a command such as “empty myfile”, and the file will no longer have content. Keep in mind that using >> in place of > would leave the file intact – basically adding nothing to the end of it.
Using echo -e
The echo command’s -e option (enable interpretation of backslash escapes) interprets a backslash followed by some other character (e.g., b) in a way that alters the output. For example, inserting instances of b in a string will cause the characters preceding b to be removed (backspaced over). Here’s an example:
$ motto="When blife bhands byou blemons, bmake blemonade" $ echo -e $motto Whenlifehandsyoulemons,makelemonade
If the text includes linefeed (n) characters instead of backspace characters, the command turns the text into multiple lines instead – basically creating newlines.
$ motto2='When nlife nhands nyou nlemons, nmake nlemonade' $ echo -e $motto2 When life hands you lemons, make lemonade
Using c will suppress newline characters and, thus, truncate the output.
$ motto3="When life hands you clemons, make lemonade" $ echo -e $motto3 When life hands you $
Using t inserts tabs into the output.
$ echo $motto4 When life hands you tlemons, tmake lemonade $ echo -e $motto4 When life hands you lemons, make lemonade
The man page for the echo command will show you all of the sequences that you can use with the -e option.
\ backslash a alert (BEL) b backspace c produce no further output e escape f form feed n new line r carriage return t horizontal tab v vertical tab NNN byte with octal value NNN (1 to 3 digits) xHH byte with hexadecimal value HH (1 to 2 digits)
Using echo -E in place of echo -e disables escape characters and is default – the same as echo without arguments.
Probably the most fun of the echo -e options is using the v (vertical tabs) option. Your command and output will look something like this:
$ motto5="Whenvlifevhandsvyouvlemons,vmakevlemonade" $ echo -e $motto5 When life hands you lemons, make lemonade
Changing font colors with echo
You can also change font colors using the echo command. For example, the command below will display text in red.
$ echo -e " 33[31mThis is some red text"
This is some red text
Use the reset command to set the font color back to normal. That