Introduction
The echo
command is a built-in feature in Linux that prints out its arguments as standard output. It is used to display text strings or the command results.
This tutorial explains different ways to use the echo
command in Linux through various examples.
Prerequisites
- A system running Linux (this tutorial uses Ubuntu 22.04).
- Access to the terminal.
Echo Command Syntax
The echo
command in Linux displays a string provided by the user. The echo
command syntax is:
echo [option] [string]
Echo Command Options
The echo
command has several arguments. The following table presents commonly used echo
command options.
Option | Description |
---|---|
-n | Displays the output while omitting the newline after it. |
-E | The default option. Disables the interpretation of escape characters. |
-e | Enables the interpretation of escape characters. |
--help | Displays a help message with information about the echo command and its options. |
--version | Prints the echo command version information. |
echo Command Examples
The echo
command prints text or variables in the terminal. It's commonly used in scripts and command-line operations to provide feedback, print messages, or output variable values. The following text presents ways to use the echo
command in Linux.
Printing a String
Run the following command to print Hello, World!
as the output:
echo Hello, World!
Using echo
without any options prints a string as is, without any changes to the output.
Changing the Output Format
The -e
option is used with escape characters, as it enables their use in the output. The escape characters are useful for formatting output and adding special characters or effects to text displayed by the echo
command. Escape characters used with the -e
option are presented in the table below.
Escape Character | Description |
---|---|
\\ | Displays a backslash character. |
\a | Plays a sound alert when displaying the output. |
\b | Removes all the spaces between the text. |
\c | Omits any output following the escape character. |
\n | Adds a newline character to the output, which signifies the end of one line of text and the beginning of a new line. |
\r | Performs a carriage return, which moves the cursor to the beginning of the current line without advancing to the next line. |
\t | Creates horizontal tab spaces. |
\v | Creates vertical tab spaces. |
For instance, using \c
lets you shorten the output by omitting the part of the string that follows the escape character:
echo -e 'Hello, World! \c This is PNAP!'
Note: If you are using the -e
option, enter your string enclosed in single quotation marks. This ensures that escape characters are interpreted correctly.
Use \n
any time you want to move the output to a new line:
echo -e 'Hello, \nWorld, \nthis \nis \nPNAP!'
Add horizontal tab spaces by using \t
:
echo -e 'Hello, \tWorld!'
Use \v
to create vertical tab spaces:
echo -e 'Hello, \vWorld, \vthis \vis \vPNAP!'
Using ANSI escape sequences lets you change the output text color:
echo -e '\033[1;37mWHITE'
echo -e '\033[0;30mBLACK'
echo -e '\033[0;31mRED'
echo -e '\033[0;34mBLUE'
echo -e '\033[0;32mGREEN'
Writing to a File
Use >
or >>
with the echo
command to print the output to a file instead of displaying it in the terminal. If the specified text file doesn't already exist, this command creates it. Run the following:
echo -e 'Hello, World! \nThis is PNAP!' >> test.txt
The command has no output. Use the cat command to display the file content:
cat test.txt
Note: Using >
overwrites the content of the text file with the new string while >>
adds the new string to the existing content.
Writing to a File and the Terminal
The echo command piped with the tee command is used to simultaneously display content in the terminal and store it in a file. For example, run:
echo "Hello, world!" | tee output.txt
To verify the output was also written to the file, run the following command:
cat output.txt
Displaying a Variable Value
The echo
command is also used to display variable values as output. For instance, to display the current user name, use:
echo $USER
Showing Multiple Variable Values
Another option is to declare multiple variables and then use the echo command to display its value. For instance, declare two variables, name
and age
:
name="Sara"<br>age=33
Next, use the echo
command to print a sentence that incorporates both variables. For example, run the following:
echo "Hello, my name is $name and I am $age years old."
This command uses double quotes to allow variable expansion within the string. This means that any variables within the double-quoted string will be replaced by their values. In this case, the shell replaces $name
with "Sara
" and $age
with 33
.
Displaying Command Outputs
The echo
command allows you to include the result of other commands in the output. For instance, run the ls command to list all the files and directories in the Home directory by using:
echo "This is the list of directories and files on this system: $(ls)"
Omitting Trailing Newline
The -n
option in the echo
command omits the trailing newline added at the end of the output. This means the text is printed without moving to a new line afterward.
This is useful when you want to print a prompt or message and then allow the user to input data on the same line. For example:
echo -n "Enter your name: "
Here, echo -n "Enter your name: "
prints the prompt without a newline, so the user's input appears on the same line as the prompt.
The read command waits for the user to type something and press Enter. Whatever the user types is stored in the variable $name
.
The echo "Hello, $name!"
command prints a greeting message that includes the $name
variable value. Since we entered Sara
at the prompt, the output is Hello, Sara!
.
Testing Dangerous Commands
Using echo
before a potentially dangerous command, such as rm -rf
, allows you to see what files and directories are affected without executing the command. For example, display the potential files and directories that match the pattern "f*
" in the current directory without deleting them:
echo rm -rf f*
Listing the Current Directory Contents
The echo
command, without any options, prints the names of all files and directories in the mentioned directory. It behaves similarly to the ls command. See the current directory contents with:
echo */
Listing Specific Types of Files
Use echo
to print only certain file types. For instance, print only .txt files in the current directory with:
echo *.txt
Conclusion
This tutorial explained how to use the echo
command in Linux with the help of practical examples. It also elaborated on echo
syntax and options.
For more Linux commands, check out our Linux Command Cheat Sheet.