How to Use the Linux Command echo and Its 3 Options

The Linux Command echo is a simple yet highly useful command. In this post, we’ll explore the basic usage of the echo command and its various options. Additionally, we’ll delve into some hidden features of the echo command and how to practically apply them in everyday scenarios.

What is the Linux Command echo?

The echo command is used to display a given string on the screen. It can also be used to save specific strings to a file or display certain variables. Let’s start by looking at the basic usage.

Basic Usage

The basic usage of the echo command is very straightforward. Simply type echo followed by the string you want to display.

echo [string to display]
ShellScript

Let’s take a basic example to print “Hello, World!”.

echo Hello, World!
ShellScript

When you run the above command, the terminal will display the string “Hello, World!”. As shown, echo is primarily used to output text.

Figure 1. Linux Command echo: Displaying a string
Figure 1. Linux Command echo: Displaying a string

Now let’s look at the main options you can use with the echo command.

Main Options

The echo command has several options. Each option allows you to fine-tune the output of the echo command.

-n Option

By default, the echo command automatically adds a newline at the end of the output. The -n option suppresses this newline, so the cursor stays on the same line, allowing the next output to continue on the same line.

echo -n No new line!
ShellScript

You can see in the image below how the command prompt appears immediately after the output.

Figure 2. Linux Command echo: Using the -n option to avoid line break
Figure 2. Linux Command echo: Using the -n option to avoid line break

-e Option

The -e option enables the interpretation of special characters like backslashes (). For instance, newline (\n) and tab (\t) characters can be used.

echo -e "First\tLine\nSecond\tLine"
ShellScript

Running the above command shows the tab and newline characters applied in the output.

Figure 3. Linux Command echo: Using the -e option to interpret special characters
Figure 3. Linux Command echo: Using the -e option to interpret special characters

-E Option

The -E option is the default setting, which means that backslashes are not interpreted as special characters but are displayed as-is. Think of it as the opposite of the -e option. Thus, using this option, \n and \t are displayed as plain text.

echo -E "No interpreting Special Characters: \n \t"
ShellScript

In the image below, you can see that using the -E option and using no option at all results in \n and \t being displayed as literal text without special interpretation.

Figure 4. Linux Command echo: Using the -E option to avoid interpreting special characters
Figure 4. Linux Command echo: Using the -E option to avoid interpreting special characters

Interpretation of Special Characters

When using the echo -e option, the following special characters can be utilized:

  • \n: New line
  • \t: Horizontal tab
  • \v: Vertical tab
  • \\: Backslash
  • \b: Backspace
  • \r: Carriage return
  • \c: Suppress further output (remaining characters are not displayed)
  • \f: Form feed (moves cursor to the next line)

There are more special characters, but the ones listed above are the most commonly used.

Practical Usage Examples

Writing Strings to a File

You can use the echo command to write strings to a file. This is done using the redirection (>) operator.

The following command creates a file named example.txt and writes the string to it. If the file already exists, it overwrites the existing content.

echo "This is an example!" > example.txt
ShellScript

The result of the above command is shown in the image below, along with the content of the created text file. This is a simple and useful way to create text files.

Figure 5. Linux Command echo: Using the redirection (>) operator to save a string to a file
Figure 5. Linux Command echo: Using the redirection (>) operator to save a string to a file

Displaying Variable Values

You can use the echo command to display the values of variables. In the following code, a string is stored in a variable named name, and the echo command is used to display this variable. When using the echo command with variables, you must prepend the variable name with a dollar sign ($) to recognize it as a variable.

name="Olivia"
echo "Hello, $name!"
ShellScript

You can see that Olivia, stored in the variable name, is displayed where $name is indicated.

Figure 6. Linux Command echo: Displaying variable values
Figure 6. Linux Command echo: Displaying variable values

This method of using and displaying variables is more commonly used in Shell scripts to output specific values to the screen.

Precautions

When using the echo command to display special characters, you must use the -e option.

When using the redirection operator, if the target file already exists, its content will be overwritten. As a result, the original information will be deleted and cannot be recovered. So, be careful when dealing with important files.

Summary

In this post, we explored the basic usage and various options of the Linux Command echo. Although echo is a simple command for displaying strings, it can be extremely useful when combined with different options. Try using echo to write strings to files or display variables in various scenarios. This will help you work more efficiently in a Linux environment.

To get accustomed to Linux commands, it’s essential to practice using them in different situations. Experiment with the echo command and its various options to understand how it works. This will help you perform tasks more proficiently in a Linux environment.

References

Leave a Comment