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.
Table of Contents
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:
The following will be displayed as output. Let’s understand the help page part-by-part.
Linux echo Command Syntax
$ echo [OPTION]... [STRING]...
Linux echo Command Examples
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.
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.
- ‘\\’ backslash character
- ‘\a’ alert (BEL): A sound is played when this character is encountered in an inputted string.
- ‘\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.
- ‘\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.
- ‘\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.
- ‘\r’ carriage return: Characters before
'\r'in a string are omitted from the output, as shown in the example below.
- ‘\t’ horizontal tab: A tab space is created in the output, wherever
'\t'appears in the string.
- ‘\v’ vertical tab: A vertical tab space is created in the output, wherever
'\v'appears in the string.
- -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
- 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.
- 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
- We can use echo command to display user-defined variables or even environment variables.
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.