The echo command (a bash built-in) is among the very primary instructions on Linux. As with ls and pwd, you may’t sit on the command line very lengthy with out utilizing it. On the similar time, echo has fairly a couple of makes use of that many people by no means benefit from. So, this publish seems into the numerous methods you should utilize this command.
What’s echo?
Principally, echo is a command that may show any textual content that you simply ask it to show. Nevertheless, if you sort “echo howdy”, the echo command is not solely spitting out these 5 letters, it is really sending out six characters – the final one being a linefeed. Let’s take a look at a few instructions that make this apparent.
First, utilizing echo with no arguments does this:
$ echo $
Discover that an empty line is displayed earlier than the immediate comes again. In the event you redirect the command’s output to the od -bc command, it’s simple to substantiate that the “invisible” output is just a linefeed – that n or octal 012 within the output under.
$ echo | od -bc 0000000 012 n 0000001
That n is the rationale that the echo command will also be used so as to add a linefeed to the tip of a file that, for some cause, lacks one. Right here’s a file that lacks a linefeed adopted by the command that provides 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 within the above instance shows the content material of recordsdata in each octal and character format. The output above reveals {that a} linefeed was added to the tip of the file. The od -bc command supplies a really helpful method to study the contents of a file, particularly when utilizing the cat command doesn’t present you every thing it’s essential to see.
> vs >>
One vital factor to concentrate to when utilizing the echo command is that > and >> work with recordsdata in a different way. The > will overwrite the content material of a file, whereas >> will append to it.
echo -n
The echo -n command will omit the linefeed from its output. That is usually utilized in scripts when prompting the person to supply a solution to some query. This works finest when the immediate and the area the place the reply can be entered are on the identical line. For instance, if the person ought to present the identify of the file to be processed by the script, we’re prone to see strains like these:
echo -n "file identify> " learn file
Whoever runs the script can be prompted to enter a file identify and can sort the file identify as proven right here:
file identify> datafile
Emptying recordsdata
The echo command will also be used to empty recordsdata. Whereas it’s extra frequent to make use of a command akin to cat /dev/null > filename, the command echo -n > filename works as effectively. Each instructions change the content material of a file with … nothing! To show this command into an alias, use a command like this or add it to your .bashrc file.
$ alias empty='echo -n > '
After organising the alias, you may simply sort a command akin to “empty myfile”, and the file will not have content material. Needless to say utilizing >> rather than > would depart the file intact – principally including nothing to the finish of it.
Utilizing echo -e
The echo command’s -e possibility (allow interpretation of backslash escapes) interprets a backslash adopted by another character (e.g., b) in a means that alters the output. For instance, inserting situations of b in a string will trigger the characters previous b to be eliminated (backspaced over). Right here’s an instance:
$ motto="When blife bhands byou blemons, bmake blemonade" $ echo -e $motto Whenlifehandsyoulemons,makelemonade
If the textual content contains linefeed (n) characters as a substitute of backspace characters, the command turns the textual content into a number of strains as a substitute – principally creating newlines.
$ motto2='When nlife nhands nyou nlemons, nmake nlemonade' $ echo -e $motto2 When life arms you lemons, make lemonade
Utilizing c will suppress newline characters and, thus, truncate the output.
$ motto3="When life arms you clemons, make lemonade" $ echo -e $motto3 When life arms you $
Utilizing t inserts tabs into the output.
$ echo $motto4 When life arms you tlemons, tmake lemonade $ echo -e $motto4 When life arms you lemons, make lemonade
The person web page for the echo command will present you the entire sequences that you should utilize with the -e possibility.
backslash a alert (BEL) b backspace c produce no additional output e escape f kind feed n new line r carriage return t horizontal tab v vertical tab NNN byte with octal worth NNN (1 to three digits) xHH byte with hexadecimal worth HH (1 to 2 digits)
Utilizing echo -E rather than echo -e disables escape characters and is default – the identical as echo with out arguments.
Most likely probably the most enjoyable of the echo -e choices is utilizing the v (vertical tabs) possibility. Your command and output will look one thing like this:
$ motto5="Whenvlifevhandsvyouvlemons,vmakevlemonade" $ echo -e $motto5 When life arms you lemons, make lemonade
Altering font colours with echo
You may also change font colours utilizing the echo command. For instance, the command under will show textual content in purple.
$ 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