The echo Command in Linux (With Examples)

Try this guide with our instant dedicated server for as low as 40 Euros

echo command in linux

By providing your email address or using a single sign-on provider to create an account, you agree to our Terms of Service and that you have reviewed our Privacy Policy.

Thank you for contacting us! We will get in touch with you soon.

Regardless of the distribution, the echo command in Linux is the go-to option to display a line of text, or a string passed as an argument. Widely utilized in batch files and shell scripts, this built-in command outputs status messages to either the screen or a file.

In a Linux environment, echo is a fundamental command used in system configuration and control processes. It plays a pivotal role in generating status text messages within batch files and bash scripts, ensuring that the user receives accurate and timely feedback about the success or failure of the operations. 

As you can imagine, without echo, shell programs would lack a mechanism for delivering visible feedback. As such, many experts consider it an integral aspect of adding a feedback interface to critical commands like head, tail, and grep.

The standard functionality of echo in Linux scripting is similar to the output capabilities (for instance, the print() function in Python) of programming languages. During its operation, echo accepts input in string form and returns the result. This operation remains consistent across different platforms and environments.

Table Of Content

  1. The Syntax of the Echo Command In Linux
    1. Options For The Echo Command In Linux
  2. The 5 echo Command In Linux Examples
    1. The Prerequisites
    2. Example #1: Change the Format of the Output
    3. Example #2: Change the Color of the Output
    4. Example #3: Write the Output to a File
    5. Example #4: Output Variable Values
    6. Example #5: Display the Command Results
  3. How Can You Set the Text Colors in the Terminal?
  4. How Can You Set The Text Properties?
  5. Conclusion
  6. FAQs

The Syntax of the Echo Command In Linux

The standard echo command in Linux follows the following syntax:

# echo [option] [string]

echo option string

Options For The Echo Command In Linux

The easiest way of viewing the list of all supported echo command options, use the –help argument with the command:

# /bin/echo --help

bin echo help

The following are the most commonly used options for the echo command:

  • -n: Removes the new line that follows the output of the command. 
  • -E: By default, the meaning of escape characters is disabled.
  • -e: allows the following escape characters to be interpreted
    • \\: Shows the backslash symbol (\).
    • \a: Plays an alert sound when the output is displayed on the screen.
    • \b: Creates a backspace character.
    • \c: Delete all output following the escape character.
    • \e: The output is similar to hitting the Esc key.
    • \f: The cursor automatically moves to the start of the next page when it detects the form feed character.
    • \n: A new line is added to the output.
    • \r: Takes out a carriage return.
    • \t: Makes horizontal tab gaps.
    • \v: Makes vertical tab gaps.
    • \NNN: Byte encoding NNN’s octal value.
    • \xHH: The hexadecimal value of HH in a byte.

The 5 echo Command In Linux Examples

Let’s now discuss the four examples that illustrate the capabilities of the echo command.

The Prerequisites

  • A system running a mainstream Linux distribution
  • A user account with root or sudo privileges

Example #1: Change the Format of the Output

By default, the echo command prints the string provided as the input. 

You can modify this default behavior with the -e flag that customizes the output by customizing how the command outputs the special characters. 

For example, you may reduce the output size by using \c to remove the part of the string that comes after the escape character:

# echo -e 'Hello, World! \c This is REDSWITCHES!'

echo -e hello world etc

Use \n to move the output to a new line:

# echo -e 'Hello, \nWorld, \nthis \nis \nREDSWITCHES!'

echo -e hello nworld nthis

Use \t to add horizontal tab spacing.

# echo -e 'Hello, \tREDSWITCHES!'

echo -e hello tredswitches

To add vertical tab spaces, use \v:

# echo -e 'Hello, \vWorld, \vthis \vis \vREDSWITCHES!'

echo -e hello vworld vthis

Example #2: Change the Color of the Output

By default, echo follows the standard color scheme of the terminal. However, you can use ANSI escape sequences to change the color of the output text. This is a great way of distinguishing various types of output by adding colors. 

Here are several examples of changing the color of the output of the echo command in the terminal:

# 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

Example #3: Write the Output to a File

The standard behavior of the echo command is outputting the command to the standard output (in most cases, the terminal). 

However, you can redirect the output of the command to a local file using the standard redirection operator (> or >>).

# sudo echo -e 'Hello, World! \nThis is REDSWITCHES!' >> test.txt

This command creates the test.txt text file if it doesn’t already exist and adds the provided text to the file. 

Next, use the cat command to view the contents of the file:

# cat test.txt

cat test.txt

Example #4: Output Variable Values

A common use of the echo command is to output the value of the system variables. For instance, to print the name of the active user, type:

# echo $USER

echo $user

Example #5: Display the Command Results

You can include the output of other commands in the output of the echo command. The syntax of the echo command in this context is as follows:

# echo "[string] $([command])

  • [string]: The string that you wish echo to include
  •  [command]: To display the result, combine the command with the echo command.

For instance, use the following to get a list of every file and directory in the Home directory:

# echo "This is the list of directories and files on this system: $(ls)"

directory list and files on system

Also Read: lsof Command in Linux with Examples

Also Read: A Guide on How to Install and Use Linux Screen Command in 2024

How Can You Set the Text Colors in the Terminal?

The echo command supports text properties such as font and background colors in the terminal output. In some implementations, the echo command automatically adds colors to specific areas of the output. For instance, the variable or file names are highlighted in a different color to distinguish them from the rest of the content. 

The following table presents an overview of unique escape codes for unique backgrounds and text colors.

use of echo to identify colors

Also Read: 13 Examples That Showcase the xargs Command in Linux

How Can You Set The Text Properties?

Like colors, the echo command also supports text properties like font display. Using the following character sequences, you can make the text bold, underlined, and have a colored background. 

Consider the following table that presents essential character sequences:

use of echo to set text properties

Also Read: 6 Simple Examples of Using the Linux watch Command

Conclusion

The Linux echo command is a useful and indispensable tool for text production on the console. We have examined its basic use and some of its more sophisticated features, such as escaping characters and using variables, through several examples. 

The command helps with scripting and automation activities, making static text display easier. It is an essential part of the Linux environment for all levels due to its simplicity and efficacy. Knowing the various uses for the echo command improves proficiency with the command-line interface, allowing efficient communication and automation.

Whether you’re a beginner or an experienced user, our dedicated hosting services provide the fundamental support you need. Choose RedSwitches for a hosting journey that’s as efficient and powerful as the echo command in Linux. 

We offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. What is the purpose of the echo command in Linux?

To see text on the terminal in Linux, use the echo command. It is an easy-to-use yet effective tool for printing text-based data, variables, and messages.

Q. How do I use the echo command to print a message to the terminal?

Simply type echo followed by the message in single or double quotes. For example: echo ‘Hello, Linux!’

Q. Can I use variables with the echo command?

Yes, the echo command allows the inclusion of variables. For instance: name=”John” and then echo “My name is $name”

Q. How do I print special characters with the echo command?

To print special characters or escape sequences, use the -e option with echo. For example: echo -e “This is a new line\n”

Q. Can the echo command be used within shell scripts for automation?

Absolutely. The echo command is commonly used in shell scripts to display messages, status updates, or variable values during script execution.

Q. How can I redirect the echo command output to a file?

Use the operators >> or > to reroute the output. ‘Text to be stored’ is an example of this: echo output.txt

Q. Are there any limitations to the echo command?

While versatile, the echo command may not be suitable for complex formatting or handling large amounts of data. In such cases, other commands like printf may be more appropriate.

Q. Can I combine multiple echo commands on a single line?

Semicolons may be used to concatenate numerous echo instructions on a single line. As an illustration, echo “First”; echo “Second.”

Q. How can I suppress the newline character when using the echo command?

To avoid appending a newline character, use the -n option. For instance: echo -n “This will be on the same line”

Q. Are there any security considerations when using the echo command?

Watch out for possible security flaws like code injection while repeating user input. User input should always be validated and sanitized to avoid unexpected outcomes.

Try this guide with our instant dedicated server for as low as 40 Euros