echo command is built in command in Linux, that prints text/string passed to this command as an argument on the terminal as an output. In other words, you can say “echo is a command that will display any text that you ask it to display” or “echo command used to display a string provided by the user”.

In this tutorial, you will learn about echo command in Linux and uses of echo command with examples.

Uses of echo command

  • Echo is frequently used in code that is written as shell scripts.
  • It is also used to display text/strings or command results as messages.

Syntax of echo command

syntax for echo command is:

echo [option] [string]

Options available with echo.

    1. -n: Do not output the trailing newline
    2. -e: Enable interpretation of backslash escapes
    3. -E: Disable interpretation of backslash escapes (default)
        • \\ : Backslash
        • \a: Alert (BEL)
        • \b: Backspace
        • \c: Produce no further output
        • \e: Escape
        • \f: Form feed
        • \n: New line
        • \r: Carriage return
        • \t: Horizontal tab
        • \v: Vertical tab
        • \0NNN: Byte with octal value NNN (1 to 3 digits)
        • \xHH: Byte with hexadecimal value HH (1 to 2 digits)

echo help

To get help related to echo and display all options available for echo command use the command /bin/echo --help. Simply using echo --help will display –help message.

 echo --help

echo help

/bin/echo --help

bin echo help
To get version information for echo use the command /bin/echo --version. Simply using echo --version will display –version message.

echo --version
/bin/echo --version

bin echo version
Using echo | Examples of echo command

1. echo command without any option displays exactly the same string as it is provided by the user.

echo "Welcome To Programbr Tutorial"

echo command in Linux
2. By default, the echo command’s output adds new line in output, such that the output is displayed as a single continuous line. If you wish not to add any new line, use -n option with echo. The syntax for not add any trailing newline is:

echo -n "[string]"
echo -n "Welcome To Programbr Tutorial"

echo -n command in Linux

Enable interpretation of backslash escapes |Using escape characters

Using the -e option allows you to use escape characters. With escape characters, you can display your text as output in a different manner of your choice.

3. Using sequence \b (backspace) with echo,  will remove all the spaces in between the text.

echo -e "Welcome \bTo \bProgrambr \bTutorial"

echo -e b command in Linux
4. Use the sequence \a (alert) with echo, to have a sound alert

echo -e "\aWelcome To Programbr Tutorial"

echo -e a command in Linux
5. Use the sequence \t (horizontal tab) with echo, to have horizontal tab spaces.

echo -e "Welcome \tTo \tProgrambr \tTutorial"

echo -e t command in Linux

6. Use the sequence \n (new line) with echo, to text to span into multiple new lines.

echo -e "Welcome \nTo \nProgrambr \nTutorial"

echo -e n command in Linux

7. Use the sequence \v (vertical tab) with echo, to have vertical tab spaces.

echo -e "Welcome \vTo \vProgrambr \vTutorial"

echo -e v command in Linux

8. Use sequence \c (produce no further output) with echo, to omit the portion of text after the additional parameter and suppress the trailing new line.

echo -e "Welocme \cTo programbr Tutorial"

Here echo -e with -/c sequence only printing Welocme and omitted the portion of text To programbr Tutorial and also suppress the trailing new line.

echo -e c command in Linux

9. Use sequence \r (carriage return) with echo, omit the portion of text prior to the additional parameter.

echo -e "Welcome \rTo Programbr Tutorial"

Here echo with \r sequence only printing To Programbr Tutorial and omitted the portion of text Welcome.

echo -e r command in Linux
10. It is similar to ls command. It prints all the files and folders of pwd (present working directory)

echo *

echo star command in Linux

 

Sharing is Caring
Scroll to Top