How to Use the echo Command in Linux

Updated on November 21, 2023
 How to Use the echo Command in Linux header image

Introduction

The echo command in Linux is used to display text or variables to the standard output (STDOUT). It's commonly used in shell scripts, command pipelines, and interactive shell sessions to print messages, display variables, or generate output for further processing.

This article explains how to use the echo command in Linux to display and manipulate text.

Syntax of echo Command

Below is the basic echo command syntax:

console
$ echo [options] [text...]

Within the above command, [options] includes optional flags that modify the command's behavior, and [text...] sets the text or variables to display. Multiple text arguments are separated with spaces.

echo Command Options

Option Description
-e Enables the interpretation of backslash escapes.
-n Disables output of trailing newline characters.

Practical Examples of the echo Command

  1. Display a sample message using the echo command.

    console
    $ echo "Hello, World!"
    

    The above command outputs the message Hello, World!.

    Output:

    echo command

  2. Create a new variable and display it's value using the echo command.

    console
    $ name="John" && echo "Welcome, $name"
    

    The above command outputs the message Welcome, John by substituting the value of the variable $name into the output.

    Output:

    echo variable command

  3. Display a new message with the system date.

    console
    $ echo "Today is" $(date)
    

    The above command outputs the message Today is and the output of the date command which displays the system date and time.

    Output:

    echo multiple message

  4. Display special characters.

    console
    $ echo -e "Line 1 \nLine 2 \nLine 3"
    

    The above command outputs three lines with the newline character \n sequence.

    Output:

    echo new line

  5. Display text without a trailing newline.

    console
    $ echo -n "Hello, "
    $ echo "World!"
    

    The above command outputs Hello, World! without a newline character after Hello,.

    Output:

    without trailing newline

  6. Display content without spaces between text.

    console
    $ echo -e "This \bis \ba \bexample \btext"
    

    The above command outputs the backspace escape character \b to remove spaces between words.

    Output:

    without the spaces

  7. Display tabs between text.

    console
    $ echo -e "This \tis \ta \texample \ttext"
    

    The above command output the tab escape character \t to insert tabs between words.

    Output:

    tabs between text

  8. Display all files and folders in the working directory.

    console
    $ echo *
    

    The above command outputs all files and directories in the active working directory similar to the ls command.

    Output:

    display all files/folders

Advanced Usage Scenarios

  1. Combine echo with command substitution.

    console
    $ echo "The current directory is: $(pwd)"
    

    The above command outputs the message The current directory is: and the output of the pwd command which displays the active working directory.

    Output:

    echo with command substitution

  2. Redirect the echo command output to a file.

    console
    $ echo "Hello, World!" > output.txt
    

    The above command outputs the command output Hello, World! to a new file output.txt.

    Output:

    redirect output to file

  3. Display environment variables.

    console
    $ echo "PATH: $PATH"
    

    The above command outputs the value of the PATH environment variable.

    Output:

    echo environment variable

  4. Display a colored output.

    console
    $ echo -e "\e[31mThis is red text\e[0m"
    

    The above command outputs the text in red using escape sequences to enabled a colored output.

    Output:

    colored output

  5. Using echo in a script:

    • Create a new sample script file my_script.sh using a text editor such as Nano

      console
      $ nano my_script.sh
      
    • Add the following contents to the file.

      bash
      #!/bin/bash
      echo "Starting the script..."
      echo "Current directory: $(pwd)"
      echo "Script completed."
      
    • Enable execute privileges on the file.

      console
      $ chmod +x my_script.sh
      
    • Run the script

      console
      $ ./my_script.sh
      

    The above script uses the echo command to display messages at different intervals.

    Output:

    echo in script

Conclusion

You have used the echo command in Linux to efficiently display text, variables, and messages. For more command options, run the man echo command to view the echo manual pages.