How to Output Text to the Screen Using the Linux Echo Command

The Linux Echo Command
The Linux Echo Command.

This guide will show you how to output text to the terminal window using the Linux echo command.

Used on its own in the terminal the echo command isn't particularly useful but when used as part of the script it can be used to display instructions, errors, and notifications.

Example Uses Of The Linux Echo Command

In its simplest form the easiest way to output text to the terminal is as follows:

echo "hello world"

The above command outputs the words "hello world" to the screen (minus the quotation marks).

By default, the echo statement outputs a new line character at the end of the string. 

To test this out try the following statement in a terminal window:

echo "hello world" && echo "goodbye world"

You will see that the result is as follows:

hello world

goodbye world

You can omit the new line character by adding the minus n switch (-n) as follows:

echo -n "hello world " && echo -n "goodbye world"

The result from the above command is as follows:

hello world goodbye world

Another thing to think about when using the echo statement is how it handles special characters.

For example try out the following in a terminal window:

echo "hello world\r\ngoodbye world"

In an ideal world the \r and \n would act as special characters to add a new line but they don't. The result is as follows:

hello world\r\ngoodbye world

You can enable special characters using the echo command by including the -e switch as follows:

echo -e "hello world\r\ngoodbye world"

This time the result will be as follows:

hello world

goodbye world

You could of course be in the situation whereby you are trying to output a string that would be handled as a special character and you don't want it to. In this scenario use a capital e as follows:

echo -E "hello world\r\ngoodbye world"

Which special characters are handled using the -e switch?

  • \\ backslash
  • \a alert
  • \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 octal value with 1 to 3 digits
  • \xHH byte with hexadecimal value

Let's try a couple of these out. Run the following command in a terminal:

echo -e "hel\blo world"

The above command would output the following:

helo world

Obviously not really what you would want to output to the screen but you get the point that backslash b removes the preceding letter.

Now try the following in a terminal window:

echo -e "hello\c world"

This command outputs everything up until the backslash and c. Everything else is omitted including the new line.

So what is the difference between a new line character and a carriage return? The new line character moves the cursor down to the next line whereas the carriage return moves the cursor back to the left side.

As an example enter the following into your terminal window:

echo -e "hello\nworld"

The output of the above command puts the two words on different lines:

hello

world

Now try the following out in a terminal window:

echo -e "hello\rworld"

The difference between a new line and carriage return will become very apparent as the following will be displayed as an output:

world

The word hello was displayed, the carriage return took the cursor to the beginning of the line and the word world was displayed.

It becomes a little more obvious if you try the following:

echo -e "hello\rhi"

The output from the above is as follows:

hillo

In reality using many people still use the \r\n notation when outputting to a new line. Quite often, however, you can get away with just a \n.​

Further Reading

If you would like to find out about more terminal commands try some of these guides out:

Was this page helpful?