Introduction
Before we discuss echo command in Linux, let's briefly understand-What is an Echo Command ?
The echo
command in Linux is used to display text or variables on the terminal. It is a simple yet powerful command that is widely used in scripts and command-line operations. The echo
command is essential for printing messages, displaying variable values, or redirecting output to other files or commands.
In Linux, the echo
command is one of the most basic and commonly used commands. Echo
prints the arguments it receives to standard output.
We will discuss echo
command with examples in this tutorial. We will also address a few on echo command in Linux.
echo
Command
The echo
is a shell that comes standard with Bash and most other popular shells such as Zsh and Ksh. Its behavior varies slightly from one shell to the next.
Although there is a standalone /usr/bin/echo
program, the shell's built-in version normally takes precedence. We'll look at the Bash built-in version of echo
.
The echo
command has the following syntax:
echo [-neE] [ARGUMENTS]
- The trailing newline is suppressed when the
-n
option is used. - The following backslash-escaped characters will be interpreted if the
-e
option is used:
1. \\
- Shows the backslash character.
2. \a
- Alert (BEL)
3. \b
- Displays a backspace character.
4. \c
- Suppresses any further output.
5. \e
- Displays an escape character.
6. \f
- Displays a form feed character.
7. \n
- Adds a new line to the document.
8. \r
- Represents a carriage return.
9. \t
- Makes a horizontal tab appear.
10. \v
- Displays a vertical tab.
- The
-E
option disables the escape character interpretation. This is the default behavior.
When using the echo
command, there are a few things to keep in mind.
- Before delivering the arguments to the
echo
command, the shell will substitute any variables, wildcard matches, and special characters. - Although it is not required, enclosing the arguments supplied to
echo
in double or single quotes is a good programming practice. - The literal value of each character included within the quotations will be kept when using single quotes. There will be no expansion of variables or commands.
echo
Examples
The examples below demonstrate how to use the echo command:
- On standard output, show a line of text.
echo Hello, World!
Hello, World!
- Show a line of text with a double quote in it.
Enclose a double quotation in single quotes, or escape it with the backslash letter to print it.
echo 'Hello "VegaStack"'
echo "Hello \"VegaStack\""
Output
Hello "VegaStack"
- Display a single quote in a single line of text.
To print a single quote, surround it with double quotes or use ANSI-C Quoting.
echo "I'm a Linux user."
echo $'I\'m a Linux user.'
Output
I'm a Linux user.
- Show a message with special characters in it.
To enable the interpretation of escape characters, use the -e
option.
echo -e "You know nothing, Mike.\n\t- Ygritte"
Output
You know nothing, Mike.
- Ygritte
- Characters that match the pattern
Pattern matching characters, such as wildcard characters, can be used with the echo
command. The command below, for example, will return the names of all .php
files in the current directory.
echo The PHP files are: *.php
Output
The PHP files are: index.php contact.php functions.php
- Redirect to a file
You can use the >
and >>
operators to route the output to a file instead of displaying it on the screen.
echo -e 'The only true wisdom is in knowing you know nothing.\nSocrates' >> /tmp/file.txt
The command will create the file.txt
if it does not already exist. The file will be overwritten if you use >
, whereas >>
will append the output to the file.
To view the contents of the file, use the cat command:
cat /tmp/file.txt
Output
The only true wisdom is in knowing you know nothing.
Socrates
- Displaying variables
Variables can also be displayed via echo
. We'll print the name of the presently logged-in user in the example below:
echo $USER
Output
VegaStack
Your username is stored in the shell variable $USER
.
- Displaying the output of a command
To include the command output in the echo
's parameter, use the $(command)
expression. The current date can be viewed using the following command:
echo "The date is: $(date +%D)"
Output
The date is: 14/04/22
- Displaying in color
To modify the foreground and background colors, or to set text attributes like underscore and bold, use ANSI escape sequences.
echo -e "\033[1;37mWHITE"
echo -e "\033[0;30mBLACK"
echo -e "\033[0;34mBLUE"
echo -e "\033[0;32mGREEN"
echo -e "\033[0;36mCYAN"
echo -e "\033[0;31mRED"
echo -e "\033[0;35mPURPLE"
echo -e "\033[0;33mYELLOW"
echo -e "\033[1;30mGRAY"
FAQs on Echo Command in Linux
How do I use the echo
command to display a message?
To display a static message, simply enter echo "your message"
in the command line, and it will output the message.
Can I use the echo
command to display the value of a variable?
Yes, you can use the echo
command to display the value of a variable by prefixing the variable name with a dollar sign ($
). For example, echo $VAR
will display the value of the variable VAR
.
Can I use escape sequences with the echo
command?
Yes, you can use escape sequences with the echo
command by using the -e
option. Escape sequences allow you to include special characters or formatting in the output.
How can I display the output of echo
without a trailing newline?
You can use the -n
option with the echo
command to suppress the trailing newline character. This can be useful when you want to concatenate multiple outputs on the same line or control the formatting of the output.
Can I redirect the output of echo
to a file?
Yes, you can redirect the output of the echo
command to a file by using the >
or >>
redirection operators. For example, echo "message" > file.txt
will write the output to the file file.txt
, overwriting its contents if it exists.
Is it possible to append the output of echo
to a file?
Yes, you can append the output of the echo
command to a file by using the >>
redirection operator. For example, echo "message" >> file.txt
will append the output to the file file.txt
, preserving its existing contents.
How can I display the output of echo
without interpreting escape sequences?
By default, echo
interprets escape sequences. However, you can disable escape sequence interpretation by using the -E
option with the echo
command. This can be useful when you want to display literal backslashes or other characters.
Conclusion
In conclusion, the echo
command in Linux is a fundamental tool for displaying text and variable values on the terminal. It provides a simple way to output messages, display variable contents, prompt users for input, and redirect output to files or other commands.
You should now have a decent idea of how to use the echo
command.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.