×


Echo command in Linux - With Examples

The echo command is a useful choice for writing text in a terminal window. Its main task is to display text or string but not necessarily static text.

This command is available and is commonly used in shell scripts and batch files. Also it is often used to output the results of other commands.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux system queries.

In this context, we shall look into how to use the echo command in Linux.


What is the syntax of the Echo command ?

It's syntax is given below:

$ echo [option] [string]

For example, use the following command to print Hello, World! as the output:

$ echo Hello, World!

The output will give:

Hello, World!


Echo command Options

The available Echo command Options can be displayed by using the --help argument as shown below:

/bin/echo --help

The echo command uses the following options:

  • -n: Displays the output while omitting the newline after it.
  • -E: The default option, disables the interpretation of escape characters.
  • -e: Enables the interpretation of the following escape characters:
  • \\: Displays a backslash character (\).
  • \a: Plays a sound alert when displaying the output.
  • \b: Creates a backspace character, equivalent to pressing Backspace.
  • \c: Omits any output following the escape character.
  • \e: The escape character, equivalent to pressing Esc.
  • \f: The form feed character, causes the printer to automatically advance to the start of the next page.
  • \n: Adds a new line to the output.
  • \r: Performs a carriage return.
  • \t: Creates horizontal tab spaces.
  • \v: Creates vertical tab spaces.
  • \NNN: Byte with the octal value of NNN.
  • \xHH: Byte with the hexadecimal value of HH.



1. \b remove all spaces

For Example, the below command:

$ echo -e "Hello \bWorld"

The output will be:

HelloWorld

You must use -e for the command to understand the meaning of "\"


2. \c remove the line after the option

For Example:

$ echo -e "Hello \cWorld"

The output will be:

Hello


3. \n split newline

For Example:

$ echo -e "Hello \nWorld"

The output will be:

Hello
World


4. \t separated by horizontal tab

For Example:

$ echo -e "Hello \tWorld"

The output will be:

Hello   World


5. \r remove the line before the option

For Example:

$ echo -e "Hello \rWorld"

The output will be:

World


6. \v separated by vertical tab

For Example:

$ echo -e "Hello \vWorld"

The output will be:

Hello
      World


7. echo* list all files/folders

For Example:

$ echo *


[Need help in fixing Linux system issues ? We can help you. ]


Conclusion

This article covers how to use echo command in Linux. In fact, Echo Command is used to display the text that has been passed as argument for the command. Also, it is used as a input for the next command or to send output to a file.

Syntax for using the Echo command in Linux is: 

# echo option string