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:

 

echo man page command

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

 

Linux echo help output

Linux echo Command Syntax

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

Linux echo Command Examples

example-string

 

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.

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.

     

    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.
      1. ‘r’ carriage return: Characters before 'r' in a string are omitted from the output, as shown in the example below.

     

    Linux echo carriage return example

      1. ‘t’ horizontal tab: A tab space is created in the output, wherever 't' appears in the string.
    1. echo-carriage-return-example

     

    Linux echo tab example

      1. ‘v’ vertical tab: A vertical tab space is created in the output, wherever 'v' appears in the string.
    1. echo-carriage-return-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.
      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.
    2. echo-selected-filenames

       

      Linux echo selected filenames example

    3. 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.

 

Linux echo math operations example

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

     

    echo-selected-filenames

    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.

By admin

Leave a Reply

%d bloggers like this: