Echo Command in Linux with Examples

Updated 

4 min read

The echo command is one of the most basic and frequently used commands in Linux. The arguments passed to echo are printed to the standard output.

echo is commonly used in shell scripts to display a message or output the results of other commands.

echo Command

echo is a shell builtin in Bash and most of the other popular shells like Zsh and Ksh. Its behavior is slightly different from shell to shell.

There is also a standalone /usr/bin/echo utility, but usually, the shell built-in version will take precedence. We will cover the Bash builtin version of echo.

The syntax for the echo command is as follows:

echo [-neE] [ARGUMENTS]
  • When the -n option is used, the trailing newline is suppressed.
  • If the -e option is given, the following backslash-escaped characters will be interpreted:
    • \\ - Displays a backslash character.
    • \a - Alert (BEL)
    • \b - Displays a backspace character.
    • \c - Suppress any further output
    • \e - Displays an escape character.
    • \f - Displays a form feed character.
    • \n - Displays a new line.
    • \r - Displays a carriage return.
    • \t - Displays a horizontal tab.
    • \v - Displays a vertical tab.
  • The -E option disables the interpretation of the escape characters. This is the default.

There are a few points to consider when using the echo command.

  • The shell will substitute all variables, wildcard matching, and special characters before passing the arguments to the echo command.
  • Although not necessary, it is a good programming practice to enclose the arguments passed to echo in double or single quotes.
  • When using single quotes '' the literal value of each character enclosed within the quotes will be preserved. Variables and commands will not be expanded.

echo Examples

The following examples show how to use the echo command:

  • Display a line of text on standard output.

    echo Hello, World!
    Hello, World!
  • Display a line of text containing a double quote.

    To print a double quote, enclose it within single quotes or escape it with the backslash character.

    echo 'Hello "Linuxize"'
    echo "Hello \"Linuxize\""
    Hello "Linuxize"
  • Display a line of text containing a single quote.

    To print a single quote, enclose it within double quotes or use the ANSI-C Quoting.

    echo "I'm a Linux user."
    echo $'I\'m a Linux user.'
    I'm a Linux user.
  • Display a message containing special characters.

    Use the -e option to enable the interpretation of the escape characters.

    echo -e "You know nothing, Jon Snow.\n\t- Ygritte"
    You know nothing, Jon Snow.
        - Ygritte
  • Pattern matching characters.

    The echo command can be used with pattern matching characters, such as the wildcard characters. For example, the command below will return the names of all the .php files in the current directory.

    echo The PHP files are: *.php
    The PHP files are: index.php contact.php functions.php
  • Redirect to a file

    Instead of displaying the output to the screen, you can redirect it to a file using the >, >> operators.

    echo -e 'The only true wisdom is in knowing you know nothing.\nSocrates' >> /tmp/file.txt

    If the file.txt doesn’t exist, the command will create it. When using > the file will be overwritten, while the >> will append the output to the file.

    Use the cat command to view the content of the file:

    cat /tmp/file.txt
    The only true wisdom is in knowing you know nothing.
    Socrates
  • Displaying variables

    echo can also display variables. In the following example, we’ll print the name of the currently logged in user:

    echo $USER
    linuxize

    $USER is a shell variable that holds your username.

  • Displaying output of a command

    Use the $(command) expression to include the command output in the echo’s argument. The following command will display the current date:

    echo "The date is: $(date +%D)"
    The date is: 04/17/19
  • Displaying in color

    Use ANSI escape sequences to change the foreground and background colors or set text properties like underscore and bold.

    echo -e "\033[1;37mWHITE"
    echo -e "\033[0;30mBLACK"
    echo -e "\033[0;34mBLUE"
    echo -e "\033[0;32mGREEN"
    echo -e "\033[0;36mCYAN"
    echo -e "\033[0;31mRED"
    echo -e "\033[0;35mPURPLE"
    echo -e "\033[0;33mYELLOW"
    echo -e "\033[1;30mGRAY"

Conclusion

By now, you should have a good understanding of how the echo command works.

If you have any questions or feedback, feel free to leave a comment.