In Linux, the
echo command is most common and frequently used command. It will print the passed arguments to the standard output. Generally, echo command is used in shell scripts to show a message or output the results of other commands.
By default, the
echo is built-in in Bash and other shells like Zsh and Ksh. It reacts different in different shell.
In this guide we will cover the Bash built-in version of
echo. There is also a standalone
/usr/bin/echo utility, but usually, the shell built-in version will take precedence.
The basic syntax for the echo command is as following:
echo [-neE] [ARGUMENTS]
- When the
-noption is used, the trailing newline is suppressed.
- If the
-eoption is given, the following backslash-escaped characters will be interpreted:
\\– Shows a backslash character.
\a– Alert (BEL)
\b– Displays a backspace character.
\c– Suppress any further output
\e– It displays an escape character.
\f– Displays a form feed character.
\n– It shows a new line.
\r– Show a carriage return.
\t– Display a horizontal tab.
\v– It will display a vertical tab.
-Eoption disables the interpretation of the escape characters. This is the default.
Following are points to be keep in mind while using the
- Before passing the arguments to the
echocommand, the shell will substitute all variables, wildcard matching, and special characters.
- It’s best practice to use double or single quotes to enclose the passed arguments to
echo. This is mandatory to use but it’s recommended.
- When using single quotes
''the literal value of each character enclosed within the quotes will be preserved. Variables and commands will not be expanded.
The following examples show how to use the echo command:
Print a line of text on standard output.
echo Hello, TecNStuff!
Display a line of text containing a double quote.
To print a double quote, enclose it within single quotes or escape it with the backslash character.
echo 'Hello "TecNStuff"'
echo "Hello \"TecNStuff\""
Display a line of text containing a single quote.
You should enclose the string using double quotes use the ANSI-C Quoting, to print the single quotes
echo "I'm doing good."
echo $'I\'m doing good.'
I'm doing good.
Display a message containing special characters.
-e option to enable the interpretation of the escape characters.
echo -e "If it doesn't challenge, it wont change you.\n\t- Kunj"
If it doesn't challange, it wont change you. - Kunj
Pattern matching characters.
You can use the
echo command with pattern matching characters, like the wildcard characters. For instance, below command will show the names of all the
.txt files in the current working directory.
echo The TXT files are: *.txt
The PHP files are: numbers.txt names.txt clac.txt
Output to a file
You can redirect the output to the file using the
>> operators, instead of displaying the output on the screen.
echo -e 'In a gentle way, you can shake the world. \nGandhiji' >> /tmp/file.txt
It will create a
file.txt if it doesn’t exist. If you use the
> operator it will be overwrite, while use the
>> will append the output to the file.
Use the cat command to view the content of the file:
In a gentle way, you can shake the world. Gandhiji
echo can be use to display variables. Let’s see an example to print the name of the currently logged in user:
$USER is a shell variable that holds your username.
Displaying output of a command
$(command) expression to include the command output in the echo’s argument. The following command will display the current date :
echo "The date is: $(date +%D)"
The date is: 10/20/20
You learned how to use the
echo command in Linux.
If you have any questions or feedback, feel free to leave a comment.