Linux echo Command Examples

Filed Under: Ubuntu

Linux echo command is used to display a line of text on the screen. This is done by echoing the string to the standard output.

Linux echo Command

Let’s see how the echo command is described by the Linux man page. The man page can be accessed by typing the following command:

Man Page

echo man page command

The following will be displayed as output. Let’s understand the help page part-by-part.

echo man page output

Linux echo help output

Linux echo Command Syntax

$ echo [OPTION]... [STRING]...

Linux echo Command Examples

echo command example

echo command output

There is no change in the output if the text to be displayed is written without double-quotes. However, it’s good practice to use double-quotes to emphasize the string, as shown in the example above.

echo command example 2

Linux echo Command Example

Linux echo Command Options

Linux echo Command Options can be used to modify the output of the command as per the user. Let’s go through them one-by-one.

  • -n option can be used to remove the '\n' (newline character) from the output. By default, echo includes '\n' at the end of every string it outputs.
    echo -n option

    Linux echo -n option example

    In the example above, since the newline character at the end of the output is omitted, the terminal prompts the user for input in the same line as the output.

  • -e option enables the terminal to recognize escape sequences in the inputted string.

    The following sequences are recognized, as described in the Linux man page.

    1. ‘\\’ backslash character
    2. ‘\a’ alert (BEL): A sound is played when this character is encountered in an inputted string.
    3. ‘\b’ backspace: Backspace is triggered when this character is encountered in a string. Hence, the character before '\b' is removed from the output, as shown below.
    4. echo backspace example

      Linux echo backspace example

    5. ‘\c’ produce no further output: All characters after '\c' in a string are omitted from the output. Note that the terminal prompts for input in the same line as the output. This is because '\n' – newline character is also removed from the output.
    6. echo character stop example

      Linux echo stop output example

    7. ‘\n’ newline: When encountered in a string, this escape sequence triggers the creation of a new line. As a result, characters after '\n' are outputted in a new line.
    8. echo newline example

      echo newline example

    9. ‘\r’ carriage return: Characters before '\r' in a string are omitted from the output, as shown in the example below.
    10. echo carriage return example

      Linux echo carriage return example

    11. ‘\t’ horizontal tab: A tab space is created in the output, wherever '\t' appears in the string.
    12. Linux echo tab example

      Linux echo tab example

    13. ‘\v’ vertical tab: A vertical tab space is created in the output, wherever '\v' appears in the string.
    14. echo vertical tab example

      Linux echo vertical tab example

  • -E option when used, disables the recognition of escape sequence in strings. This option is enabled by default.
  • –version and –help display version information and help window respectively and exit afterwards.

Linux echo Command Advanced Examples

  1. Linux echo command is used with the wildcard character '*' to display the names of all files present in the current directory.
    linux echo filenames

    Linux echo filenames example

    It can also be used in this way to display selected files/folders with certain lowercase/uppercase characters present in their names.

    In the example below, only files in the current directory, which have “D” as the initial letter and “s” as the last letter of their name, are displayed as output.

    echo selected filenames

    Linux echo selected filenames example

  2. Linux echo command can be used to perform basic arithmetic operations and display the result as output. This is done by enclosing the mathematical part within (( )) preceded by a $ symbol.
  3. echo maths example

    Linux echo math operations example

  4. We can use echo command to display user-defined variables or even environment variables.
    echo-variables-display-example

    echo variable value display example

Conclusion

Linux echo is a simple, fundamental command that displays a line of text as output. The output can be modified by using the available options along with the command. Among other uses, it can display filenames and values of both user-defined and system variables as well as give the result to some basic arithmetic.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages