echo Command in Linux (With Examples)

May 20, 2024


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.

echo Command in Linux with Examples


  • 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.

-nDisplays the output while omitting the newline after it.
-EThe default option. Disables the interpretation of escape characters.
-eEnables the interpretation of escape characters.
--helpDisplays a help message with information about the echo command and its options.
--versionPrints 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!
echo hello world terminal output

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 CharacterDescription
\\Displays a backslash character.
\aPlays a sound alert when displaying the output.
\bRemoves all the spaces between the text.
\cOmits any output following the escape character.
\nAdds 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.
\tCreates horizontal tab spaces.
\vCreates 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!'
echo -e \c terminal output

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!'
echo -e \n terminal output

Add horizontal tab spaces by using \t:

echo -e 'Hello, \tWorld!'
echo -e \t terminal output

Use \v to create vertical tab spaces:

echo -e 'Hello, \vWorld, \vthis \vis \vPNAP!'
echo -e \v terminal output

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'
echo -e terminal output

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
cat terminal output

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
echo tee terminal output

To verify the output was also written to the file, run the following command:

cat output.txt
terminal output for cat

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
echo $USER terminal output

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:

declaring variables

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."
printing variable value with echo

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)"
echo ls terminal output

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: "
echo -n terminal output

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.

adding user input

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!.

terminal output for echo -n command

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*
echo rm -rf terminal output

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 */
echo */ terminal output

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
echo *.txt terminal output


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.

Was this article helpful?
Sara Zivanov
Sara Zivanov is a technical writer at phoenixNAP who is passionate about making high-tech concepts accessible to everyone. Her experience as a content writer and her background in Engineering and Project Management allows her to streamline complex processes and make them user-friendly through her content.
Next you should read
How to Use the Linux sleep Command with Examples
February 3, 2021

The sleep command is used when it is necessary to postpone the execution of commands on the command line or...
Read more
How to Use the Linux tee Command
January 28, 2021

By default in Linux, a command received through standard input writes directly to standard output...
Read more
How To Use The Passwd Command In Linux
January 26, 2021

Passwords are the most important feature of security. This article explains and shows examples of how to use...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more