How to use the echo command in Linux

The echo command is a helpful tool for tasks like shell scripting, output formatting, and debugging. With it, Linux users can print to their terminal, view system variables, display environment information, and format data.

echo is a great asset for developers who like to debug on the go and in the terminal, and it works consistently across Linux distributions. It’s often paired with other commands, such as ls and pwd, to display file lists, paths, or context-specific messages during script execution.

In the following article, we’ll explore echo command usage, syntax, and options, and we’ll outline some practical examples.

Basic syntax of the echo command

At its core, the syntax of the Linux echo command is as follows:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo [option(s)] [string(s)]
echo [option(s)] [string(s)]
echo [option(s)] [string(s)]

In the above:

  • option can include flags, such as -n and -E, which are not compulsory. It can be one or more.
  • string is the text you want to print to the terminal. It can be one or more.

Understanding echo command options

Options are flags you can add to your echo command to enrich your instructions and make them more specific. They format the data that will be printed to your terminal.

  • -n: Omits trailing newlines.
  • -e: Enables backslash escapes (like \n and \t).
  • -E: Disables backslash escapes (default behavior).

When running the echo command, the options should always go after echo and before the string.

Practical echo command examples

Let’s take a look at specific Linux echo command use cases. These examples show how echo can be used for simple output, interactive scripts, formatting text, and integrating with other commands in real-world scenarios.

Printing a string

We have already seen that to print a string with the echo command you can run:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo [string]
echo [string]
echo [string]

For example, if you want to print a simple string, you can run the following on the command line:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo “Testing this thing!”
echo “Testing this thing!”
echo “Testing this thing!”

Which will print your sentence to the terminal.

You don’t need to use quotation marks (“”) with the echo command, but you can if you are used to it when writing strings.

echo is often used in conjunction with other Linux commands. For example, used together with the ls command, you can display the directory contents along with custom messages:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo "Here are the all the files in your directory:"; ls # Shows a list of your files in your current directory
echo "Here are the all the files in your directory:"; ls # Shows a list of your files in your current directory
echo "Here are the all the files in your directory:"; ls # Shows a list of your files in your current directory

This will display:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Here are all the files in your directory:
Applications YourPrivateFolder
Desktop AnotherPrivateFolder
Documents
Downloads
Here are all the files in your directory: Applications YourPrivateFolder Desktop AnotherPrivateFolder Documents Downloads
Here are all the files in your directory:

Applications    YourPrivateFolder       

Desktop        AnotherPrivateFolder       

Documents     

Downloads

Using -n to remove newline

As we previously mentioned, you can add a -n flag to your echo command to omit trailing newlines.

A common use case would involve a multi-step echo script that asks for user input. In such cases, you typically want the prompt to appear on the same line as the input field. Using echo -n helps you print the prompt without a newline.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#!/bin/bash
echo -n "Enter your username: "
read username
echo "Hello, $username!"
#!/bin/bash echo -n "Enter your username: " read username echo "Hello, $username!"
#!/bin/bash

echo -n "Enter your username: "

read username

echo "Hello, $username!"

Running the above script will return:

Using -e to enable backslash escapes

As we have seen, when using the echo command, -e enables backslash escapes, like \n (newline).

If you want to print one or more strings on multiple lines, you can run:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo -e "See you later\nalligator"
echo -e "See you later\nalligator"
echo -e "See you later\nalligator"

This will print:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
See you later
alligator
See you later alligator
See you later

alligator

Printing environment variables

You can assign your own value to a variable and then print it. To print the value of an environment variable, you can use the echo command followed by the dollar sign ($) and the variable name. For example, you can use this command to store temporary values like filenames, user input, counters, or results from other Linux commands.

Running the following:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
filename="report.txt"
echo "The file name is $filename"
filename="report.txt" echo "The file name is $filename"
filename="report.txt"

echo "The file name is $filename"

Will print:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
The file name is report.txt
The file name is report.txt
The file name is report.txt

Combining multiple strings

With the echo command, you can print multiple strings at a time. For example, running:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo "See you later" "Alligator"
echo "See you later" "Alligator"
echo "See you later" "Alligator"

Will print the two strings one next to the other:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
See you later Alligator
See you later Alligator
See you later Alligator

Redirecting output

With the Linux echo command, you can redirect output to a text file instead of displaying it on the terminal by using the > symbol. This can be useful for saving logs, messages, or command outputs into a text file for later use.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo "Message: Some message here" > file.txt
echo "Message: Some message here" > file.txt
echo "Message: Some message here" > file.txt

Here:

  1. file.txt is created, if it doesn’t already exist. Otherwise, it’s overwritten.
  2. The string containing the message is written to file.txt.
  3. The string will now be inside the file, but nothing will be printed to the terminal.

You can also append text to an existing file instead of overwriting it by using the >> operator.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo "Message: Another message!" >> file.txt
echo "Message: Another message!" >> file.txt
echo "Message: Another message!" >> file.txt

Handling special characters and escape sequences

With the echo command, you can handle special characters and escape sequences with the -e option. This allows you to add formatting to the text output, such as newlines, horizontal tab spaces, or other special symbols.

Common escape sequences include:

  • \n: Newline (it moves the output to the next line)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo -e "First line\nSecond line"
echo -e "First line\nSecond line"
echo -e "First line\nSecond line"

Prints:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
First line
Second line
First line Second line
First line

Second line
  • \t: Tab (it adds horizontal tab spaces)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo -e "Item\tPrice"
echo -e "Item\tPrice"
echo -e "Item\tPrice"

Prints:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Item Price
Item Price
Item Price
  • \r: Carriage Return (it moves the cursor to the beginning of the line)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo -e "12345\rABC"
echo -e "12345\rABC"
echo -e "12345\rABC"

Prints:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
ABC45
ABC45
ABC45
  • \\: Backslash (it inserts a literal backslash)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo -e "Path: C:\\Users\\Name"
echo -e "Path: C:\\Users\\Name"
echo -e "Path: C:\\Users\\Name"

Prints:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Path: C:\Users\Name
Path: C:\Users\Name
Path: C:\Users\Name
  • \b: Backspace (it moves the cursor one character backward)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
echo -e "helloo\b!"
echo -e "helloo\b!"
echo -e "helloo\b!"

Prints:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
hello!
hello!
hello!

Disabling backslash escapes

In the echo command, using the -E option allows you to disable the backslash escape. Imagine you are writing a script that outputs user-generated content, where you want the content to be printed out without any special interpretation of escape sequences. The standard approach would be to interpret them as their intended meaning:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
message="This\nis user-generated info!"
echo -e "$message"
message="This\nis user-generated info!" echo -e "$message"
message="This\nis user-generated info!"

echo -e "$message"

Without -E but using -e, \n is interpreted as a newline.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This
is user-generated info!
This is user-generated info!
This

is user-generated info!

However, if we do:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
message="This\nis user generated info!"
echo -E "$message"
message="This\nis user generated info!" echo -E "$message"
message="This\nis user generated info!"

echo -E "$message"

\n is interpreted literally, and there is no newline. 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
This\nis user generated info!
This\nis user generated info!
This\nis user generated info!

This can be useful in situations where escape sequences are part of the intended output, like log outputs, code snippets, JSON strings, regex, or configuration files.

For example, when outputting JSON, you want to make sure to keep it as-is:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
json='{"message": "Backup completed\\nNext backup at 03:00"}'
echo -E "$json"
{"message": "Backup completed\nNext backup at 03:00"}
json='{"message": "Backup completed\\nNext backup at 03:00"}' echo -E "$json" {"message": "Backup completed\nNext backup at 03:00"}
json='{"message": "Backup completed\\nNext backup at 03:00"}'

echo -E "$json"

{"message": "Backup completed\nNext backup at 03:00"}

Because JSON is a strict data format, it is crucial to keep it as-is, with correct escape characters and structure. Changing the formatting can break its validity, and anything that tries to read it – apps, APIs, parsers, or linters – will throw an error or behave unpredictably.

Conclusion

The echo command in Linux is an extremely versatile tool for outputting strings, manipulating text, and working with variables. In this article, we explored how different options, such as -n, -e, and -E, can handle escape characters, influence formatting, and manipulate output in a variety of scripting scenarios.

echo is a simple command that, when combined with other tools, proves increasingly useful across different contexts, from writing logs and generating JSON to building interactive scripts and managing variables. Whatever your sector, you can take full advantage of echo’s capabilities by experimenting with it, and making it an essential tool in your Linux toolbox.

The echo command in Linux FAQ

What is the echo command in Linux?

The echo command in Linux is a valuable tool for interacting with users and debugging scripts. It’s employed by developers to print text or variables to the terminal. Common tasks include displaying messages, formatting text, or outputting the value of variables in shell scripts and command-line operations.

How do I use the echo command?

You can run the echo command by typing echo [options] [string] in the terminal. For example, echo “Hello!” will print the text to the terminal. You can add options such as  -n, -e, and -E to manipulate your text, and use the dollar sign ($) to assign variables.

What is the difference between echo and cat commands?

While the echo command prints text or variables, the cat command is used to show the contents of files. echo is typically used for outputting strings, while cat is used for reading files.

Author
The author

Marta Palandri

Marta Palandri is a senior technical editor with over six years of experience as a developer, working extensively with APIs and backend systems. She now combines her development experience with her editorial background to create content focused on accessibility and storytelling. Find her on LinkedIn.