How to use Echo Command in Linux (With Examples) (2024)

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)

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.

OptionDescription
-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!
How to use Echo Command in Linux (With Examples) (2)

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!'
How to use Echo Command in Linux (With Examples) (3)

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)

Add horizontal tab spaces by using \t:

echo -e 'Hello, \tWorld!'
How to use Echo Command in Linux (With Examples) (5)

Use \v to create vertical tab spaces:

echo -e 'Hello, \vWorld, \vthis \vis \vPNAP!'
How to use Echo Command in Linux (With Examples) (6)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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)

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.

How to use Echo Command in Linux (With Examples) (2024)
Top Articles
Latest Posts
Article information

Author: Manual Maggio

Last Updated:

Views: 6638

Rating: 4.9 / 5 (49 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Manual Maggio

Birthday: 1998-01-20

Address: 359 Kelvin Stream, Lake Eldonview, MT 33517-1242

Phone: +577037762465

Job: Product Hospitality Supervisor

Hobby: Gardening, Web surfing, Video gaming, Amateur radio, Flag Football, Reading, Table tennis

Introduction: My name is Manual Maggio, I am a thankful, tender, adventurous, delightful, fantastic, proud, graceful person who loves writing and wants to share my knowledge and understanding with you.