Intro To 'echo' Command In Linux
2023-05-02 - By Robert Elder
I use the 'echo' command to print pieces of text on the terminal:
echo "Hello World!"
You can use the 'echo' command to print literal pieces of text like this:
echo "Text inside 'double' quotes."
echo 'Text inside "single" quotes.'
echo Text without quotes.
The above three echo statements will output the following:
Text inside 'double' quotes.
Text inside "single" quotes.
Text without quotes.
Evaluating Variables With 'echo'
You can also include variables and evaluate them like this:
echo "The current working directory is: $PWD"
The above echo statement will output the following:
The current working directory is: /home/robert/important
Evaluating Commands With 'echo'
You can even include the output from other commands. If you run the date command by itself:
date
the output will be something like this:
Wed 03 May 2023 11:24:47 AM EDT
If we run the 'date' command in an echo statement like this:
echo "The current date is: $(date)"
the output will look like this:
The current date is: Wed 03 May 2023 11:24:47 AM EDT
Evaluating Newlines With 'echo'
By default, the 'echo' command won't evaluate escaped characters like newlines:
echo "pear\nbanana\napple"
As you can see, the output still contains the literal '\n' characters:
pear\nbanana\napple
You need to include the '-e' flag to evaluate them:
echo -e "pear\nbanana\napple"
the output will then be the following:
pear
banana
apple
Piping The Output From 'echo'
I can pipe the output of the 'echo' command into the sort command to sort the list of lines:
echo -e "pear\nbanana\napple" | sort
apple
banana
pear
I can also pipe the output of the 'echo' command into the xxd command to see a hexadecimal representation of the output:
echo -e "pear\nbanana\napple" | xxd
00000000: 7065 6172 0a62 616e 616e 610a 6170 706c pear.banana.appl
00000010: 650a e.
And that's why the 'echo' command is my favorite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|