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:
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:
echo [string]
For example, if you want to print a simple string, you can run the following on the command line:
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:
echo "Here are the all the files in your directory:"; ls # Shows a list of your files in your current directory
This will display:
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.
#!/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:
echo -e "See you later\nalligator"
This will print:
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:
filename="report.txt" echo "The file name is $filename"
Will print:
The file name is report.txt
Combining multiple strings
With the echo command, you can print multiple strings at a time. For example, running:
echo "See you later" "Alligator"
Will print the two strings one next to the other:
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.
echo "Message: Some message here" > file.txt
Here:
- file.txt is created, if it doesn’t already exist. Otherwise, it’s overwritten.
- The string containing the message is written to file.txt.
- 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.
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)
echo -e "First line\nSecond line"
Prints:
First line Second line
- \t: Tab (it adds horizontal tab spaces)
echo -e "Item\tPrice"
Prints:
Item Price
- \r: Carriage Return (it moves the cursor to the beginning of the line)
echo -e "12345\rABC"
Prints:
ABC45
- \\: Backslash (it inserts a literal backslash)
echo -e "Path: C:\\Users\\Name"
Prints:
Path: C:\Users\Name
- \b: Backspace (it moves the cursor one character backward)
echo -e "helloo\b!"
Prints:
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:
message="This\nis user-generated info!" echo -e "$message"
Without -E but using -e, \n is interpreted as a newline.
This is user-generated info!
However, if we do:
message="This\nis user generated info!" echo -E "$message"
\n is interpreted literally, and there is no newline.
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:
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.