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.
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.
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.
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,
We can also print out all .pdf files as follows,
Or all files or directories starting with the letter D,
Assuming we have a variable a with the value of 8, i.e 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,
To print the home path,
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,
To print text with characters such as single or double quotes,
echo 'There\'s a quote in this text.'
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.'
This is a random tabbed text.
The -v option o]prints text with vertical tabs,
echo -e 'This is \va random \vvertical text.'
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'
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'
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 220.127.116.11 -c2)
We can also generate a list of strings e.g ranges,
to print all values from 1 to 10.
to print all letters from a to z.
We can also print colored text by using ANSI escape sequences,
echo -e "\033[0;33mpurple text"
or for black text,
echo -e "\033[0;30mblack text"
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.
- Execute the command /bin/echo --help for echo options.
- Execute the command man echo for echo manual page.
- Linux piping and redirection