echo command in Linux
Do not miss this exclusive book on Binary Tree Problems. Get it now for free.
In this article we discuss the basic uses of the echo command along with some uses involving it combining with other Linux commands to achieve goal.
Table of contents.
- Introduction.
- Syntax.
- Commands.
- Summary.
- References.
Introduction.
echo is a Unix/Linux tool used to display text or strings which are passed to them as arguments. It can be compared to printf in C or cout in C++.
It is commonly used for shell scripting and batch files.
Echo command in Linux is frequently used by Software Developers:
- To check the value set in an environment variable.
Syntax.
The syntax is as follows,
echo [SHORT-OPTION]... [STRING]...
echo LONG-OPTION
Its various options are,
-n, which avoids an output with a trailing new line.
-e, which enables the interpretation of backslash escape sequences.
-E, to disable the interpretation of backslash escape sequences.
\a, is used for an alert.
\b, for a backspace.
\c, after it, there is no further output.
\n, for a new line.
\t, for a horizontal tab.
\v, for a vertical tab.
\r, for a carriage return.
\e, escape character equivalent to pressing the escape button.
Commands.
A simple example to print a text to the terminal,
echo "a random text here"
The text 'a random text' will be printed to the terminal.
We can also use echo with a wildcard character * to print out all files and directories in the current working directory as follows,
echo *
We can also print out all .pdf files as follows,
echo *.pdf
Or all files or directories starting with the letter D,
echo D*
Assuming we have a variable a with the value of 8, i.e a = 8
a=8
To print its value with echo, we write,
echo 'The number $a is less than 10'
The output is
The number 8 is less than 10
PATH is an environmental variable that tells the shell which directories to search for executable files in response to commands issued by a user,
echo $PATH
To print the home path,
echo ~
To produce an alert sound we use /a,
echo -e "\aALERT"
To remove all spaces in text, we use the -b option together with the escape interpretor -e as follows,
echo -e 'This \bis \ba \brandom \bbackspaced \btext.'
The output is,
Thisisarandombackspacedtext.
To print text with characters such as single or double quotes,
echo 'There\'s a quote in this text.'
or
echo 'The \"echo\" command prints text.'
To print text in a new line we use the -n option with with the escape interpretor -e,
echo -e 'This is \na random \nnewline text.'
The output is the following,
This is
a random
newline text
We can also use tabs in the output as follows,
echo -e 'This \tis \ta \trandom \ttabbed \ttext.'
The output,
This is a random tabbed text.
The -v option o]prints text with vertical tabs,
echo -e 'This is \va random \vvertical text.'
The output,
This is
a random
vertical text
To supress a trailing new line, we use the \c option,
echo -e 'This is a random backspaced text \c the supressed text is here'
The output,
This is a random backspaced text
We can also specify a carriage return using the \r option to omit text before it,
echo -e 'This text is not printed \r After the carriage return'
The output,
After the carriage return
Echo can be used with redirection, that is, to send output to a file,
echo * > dirs.txt
The output of echo which is all files and directories in the current working directory are written to dirs.txt file.
To append the output of echo to a file, we use the >> redirection characters,
echo * >> dirs.txt
Echo can also be used to print the output of a command, e.g
echo $(ping 8.8.8.8 -c2)
We can also generate a list of strings e.g ranges,
echo {1..10}
to print all values from 1 to 10.
or
echo {a..z}
to print all letters from a to z.
We can also print colored text by using ANSI escape sequences,
An example
echo -e "\033[0;33mpurple text"
or for black text,
echo -e "\033[0;30mblack text"
Summary.
Echo repeats what we tell it to repeat, like an literal 'echo'.
We can either use it alone or combine it with other commands to produce the desired output.
References.
- Execute the command /bin/echo --help for echo options.
- Execute the command man echo for echo manual page.
- Linux piping and redirection
Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.