Echo Command in Linux With Practical Examples

In this article, I will talk about echo command in Linux with its examples and basic applications. I will also give a little information about special sequences, and quotes in Linux shell.

Note: Some shells have their own echo commands which are little different from the main echo command. This article is for bash’s echo. For other shells, there is not much difference. At the same time, I will also try my best to include these differences. And one more thing to note is that most of these commands should also work on any Unix-based system.

Table of Contents

Basic Applications of Echo Command in Linux

Syntax:

echo [-neE] [arg ...]

In the above syntax, three dots (...) means you can have as many arg as possible. The execution of the above command will print all the ‘args’ separated by space.

The simplest example:

[ajay@lenovo ~]$ echo "hello world"
Output:
hello world

In the above example, there is only one argument written under the quotes. If I remove the quotes, It becomes two separate arguments but the results would be the same. For example,

[ajay@lenovo ~]$ echo hello world
Output:
hello world

The above example might tempt you to think that anything after the echo is printed out. But that is not true. All the POSIX compliant shells terminate any command at semicolon (;). So, echo can access argument only up to the semicolon (;), after that, the shell treats all the world(s) as a separate command(s). For example given below, our shell tries to execute the command called ‘world’, but it cannot find that command and hence it throws the error:

[ajay@lenovo ~]$ echo hello world; world
Output:
hello world
bash: world: command not found

To prevent this termination at the semicolon ( ;), prepend the semicolon with a backslash as in the following example:

[ajay@lenovo ~]$ echo hello world\; world
Output:
hello world; world

Given all the above issues associated with semicolons, backslashes, and quotes, you might get confused sometimes. This is true if you are a beginner. My suggestions would be to always use double quotes with the echo.

One more thing to note is that the meaning of ‘arg’ is divers, it can be a parameter with $ (ex – $USER), or an even command (ex – $(date)), or their mix-ups as shown below:

[ajay@lenovo ~]$ echo "hello $USER"
Output:
hello ajay
[ajay@lenovo ~]$ echo "today's date is $(date)"
Output:
today's date is Tue Feb 15 12:35:40 AM IST 2022

How to Prevent Echo From Appending the Newline (\n)

In all of the above examples, as you can see, echo is inserting the newline (\n) at the end. To remove that, we need to use the flag -n.

Example:

[ajay@lenovo ~]$ echo -n "line 1"; echo -n "line 2"
Output:
line 1line 2[ajay@lenovo ~]$ 

The new line (\n) is an escape sequence. There are others as well as shown under the next heading.

Escape Sequences in Echo Command in Linux

The escape sequences in echo start with the backlash. Most of these escape sequences, very often, are valid in other commands as well (ex – printf, gawk, python, etc.).

By default, these escape sequences are disabled in bash as shown below:

[ajay@lenovo ~]$ echo "hello\nworld"
Output:
hello\nworld

To enable the interpretation of escape sequences, you need to use the flag -e:

[ajay@lenovo ~]$ echo -e "hello\nworld"
Output:
hello
world

Note: In dash shell, these escape sequences are disabled by default. On the other hand, in zsh shell, they are enabled. To disable it, use the flag -E.

After enabling the escape sequences, I will describe the types of escape sequences one by one with examples.

Backspace (\b) in Echo

The backspace sequence deletes one character ahead of it. For example,

[ajay@lenovo ~]$ echo -e "first line\bsecond line"
Output:
first linsecond line

Carriage Return (\r) in Echo

It puts the cursor at the beginning of the text. In the example given below, ‘NNNN’, then, replaces ‘firs’.

Example:

[ajay@lenovo ~]$ echo -e "first line \rNNNN"
Output:
NNNNt line 
[ajay@lenovo ~]$ 

Horizontal Tab (\t) in Echo

The horizontal tab is what you see when you press the tab button in any text editor.

Example:

[ajay@lenovo ~]$ echo -e "word1 \tword2" 
Output:
word1     word2

Vertical Tab (\v) in Echo

This is best understandable by the following example:

[ajay@lenovo ~]$ echo -e "word1 \vword2" 
Output:
word1 
      word2

Alert (\a) in Echo

This is also called a bell sequence. It does not print anything, instead, you will hear a beep sound.

[ajay@lenovo ~]$ echo -e "hello world \a"
Output:
hello world 

\c in Echo

It suppresses further output i.e. as soon as echo encounters \c, echo does not print out anything.

[ajay@lenovo ~]$ echo -e "hello\c world" 
Output:
hello[ajay@lenovo ~]$ 

Note: In the command gawk, ‘\c’ means a different thing.

Literal Backslash (\\) in Echo

In order to print a single backslash, you will need to use double backslashes. Example:

[ajay@lenovo ~]$ echo -e "hello \\ world"
Output:
hello \ world

Formfeed (\f) in Echo

Technically, it directs the printer to advance down to the next “page”. But in the terminal, you will see the output similar to the vertical tab. I have not found its use yet. If you have any, please comment below.

[ajay@lenovo ~]$ echo -e "hello \f world"
Output:
hello 
       world

Octal Number Based Sequence (\0nnn) in Echo

Echo can also print out any character corresponding to the octal number ‘nnn’. Here the number ‘nnn’ can have one to three digits. For example, ‘013’ and ’13’ correspond to a vertical tab, ‘047’ and ’47’ to a single quote, ‘042’ and ’42’ to a double quote, and so on. Many of these characters and corresponding octal numbers can be found on this link.

[ajay@lenovo ~]$ echo -e "hello \0042world\0042"
Output:
hello "world"
[ajay@lenovo ~]$ echo -e "hello \042world\042"
Output:
hello "world"

Hexadecimal Number Based Sequence (\xhh) in Echo

Here, echo prints the character corresponding to one or two digits hexadecimal number. For example, ‘0b’ or ‘b’ is the same as a vertical tab, ’26’ is the same as a single quote, and ’22’ is the same as a double quote, and so on. Other matches between hexadecimal numbers and characters can be found in the same hyperlink given above.

Examples:

[ajay@lenovo ~]$ echo -e "hello \x22world\x22"
Output:
hello "world"
[ajay@lenovo ~]$ echo -e "hello \xb world"
Output:
hello 
       world

Escape Sequences \uhhhh and \Uhhhhhhhh for Unicode Characters in Echo

Here, HHHH in \uHHHH is a hexadecimal number from one to four digits. Similarly, HHHHHHHH in \uHHHHHHHH is one to eight digits hexadecimal number.

When echo encounters these escape sequences, it prints the corresponding Unicode (ISO/IEC 10646) character.

Examples:

Echo printing Unicode glyphs in Linux
Figure: Echo printing Unicode glyphs

Application: You can check, using the above commands, if your terminal such as urxvt supports glyphs and colored glyphs.

Escape Character (\e or \E) In Echo

One very good application of the escape character is printing the output in color.

echo printing out in colors in Linux
Figure: echo printing out in colors.

Wrapping Up

That’s all with the echo command in Linux with practical examples. If there are any mistakes, or if you have any confusion, please comment below.

2 thoughts on “Echo Command in Linux With Practical Examples”

  1. Pingback: How to Use Loops in Bash? | SmartTech101

  2. Pingback: How to send notifications in linux using dunstify/notify-send | SmartTech101

Leave a Comment

Your email address will not be published. Required fields are marked *