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.
![How to use Echo Command in Linux (With Examples) (1) How to use Echo Command in Linux (With Examples) (1)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-command-in-linux-with-examples.png)
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!
![How to use Echo Command in Linux (With Examples) (2) How to use Echo Command in Linux (With Examples) (2)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-hello-world-terminal-output.png)
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!'
![How to use Echo Command in Linux (With Examples) (3) How to use Echo Command in Linux (With Examples) (3)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-e-c-terminal-output.png)
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!'
![How to use Echo Command in Linux (With Examples) (4) How to use Echo Command in Linux (With Examples) (4)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-e-n-terminal-output.png)
Add horizontal tab spaces by using \t
:
echo -e 'Hello, \tWorld!'
![How to use Echo Command in Linux (With Examples) (5) How to use Echo Command in Linux (With Examples) (5)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-e-t-terminal-output.png)
Use \v
to create vertical tab spaces:
echo -e 'Hello, \vWorld, \vthis \vis \vPNAP!'
![How to use Echo Command in Linux (With Examples) (6) How to use Echo Command in Linux (With Examples) (6)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-e-v-terminal-output.png)
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'
![How to use Echo Command in Linux (With Examples) (7) How to use Echo Command in Linux (With Examples) (7)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-e-terminal-output.png)
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
![How to use Echo Command in Linux (With Examples) (8) How to use Echo Command in Linux (With Examples) (8)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/cat-terminal-output.png)
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
![How to use Echo Command in Linux (With Examples) (9) How to use Echo Command in Linux (With Examples) (9)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-tee-terminal-output.png)
To verify the output was also written to the file, run the following command:
cat output.txt
![How to use Echo Command in Linux (With Examples) (10) How to use Echo Command in Linux (With Examples) (10)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/terminal-output-for-cat.png)
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
![How to use Echo Command in Linux (With Examples) (11) How to use Echo Command in Linux (With Examples) (11)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-user-terminal-output.png)
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
![How to use Echo Command in Linux (With Examples) (12) How to use Echo Command in Linux (With Examples) (12)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/declaring-variables.png)
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."
![How to use Echo Command in Linux (With Examples) (13) How to use Echo Command in Linux (With Examples) (13)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/printing-variable-value-with-echo-terminal-output.png)
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)"
![How to use Echo Command in Linux (With Examples) (14) How to use Echo Command in Linux (With Examples) (14)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-ls-terminal-output.png)
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: "
![How to use Echo Command in Linux (With Examples) (15) How to use Echo Command in Linux (With Examples) (15)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-n-terminal-output.png)
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.
![How to use Echo Command in Linux (With Examples) (16) How to use Echo Command in Linux (With Examples) (16)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/adding-user-input.png)
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!
.
![How to use Echo Command in Linux (With Examples) (17) How to use Echo Command in Linux (With Examples) (17)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/terminal-output-for-echo-n-command.png)
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*
![How to use Echo Command in Linux (With Examples) (18) How to use Echo Command in Linux (With Examples) (18)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-rm-rf-terminal-output.png)
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 */
![How to use Echo Command in Linux (With Examples) (19) How to use Echo Command in Linux (With Examples) (19)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-terminal-output.png)
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
![How to use Echo Command in Linux (With Examples) (20) How to use Echo Command in Linux (With Examples) (20)](https://i0.wp.com/phoenixnap.com/kb/wp-content/uploads/2024/05/echo-txt-terminal-output.png)
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.