As a beginner when you start working with Bash scripts, the first command you will probably learn and use is the echo
command. You can think of bash echo command something similar to the print
command in other programming languages. The echo
command is a bash built-in and its purpose is to print the output to stdout (i.e. Terminal).
In this article, I will show you what is bash echo command and how to use it in your shell scripts. Before we get into the topic, let us see the difference between a Bash bulit-in and an external command.
Table of Contents
Bash built-in echo vs external echo command
In Linux and other Unix-like systems, some commands are provided as both built-ins and external commands. For example, the /bin/echo
is an external command that has the same name as the echo
bash builtin.
When we call a command that exists both as a builtin and as an external command, usually the built-in is executed by default. Because, the precedence order for command names is alias
, then function
, then builtin
, and then external command in the directories listed in $PATH
in order.
If you want to force an external command that is available as both built-in and external command, simply use its full path.
To put this clearly, the following command calls the bash built-in:
$ echo <some-text>
And the following calls external echo
command:
$ /bin/echo <some-text>
To find out whether a command is a built-in or an external command, we can use type command:
$ type echo echo is a shell builtin
To display all locations containing the specified executable, use -a
flag:
$ type -a echo echo is a shell builtin echo is /usr/bin/echo
For more details about type
command usage, refer the following link:
Through out this guide, we will be discussing the bash built-in echo
command, not the external one.
Display bash echo command help
Almost all Linux commands out there have either a man page or help section to understand how to use that command. The help manual of echo
command is included in the Bash manual page
To access the man page for bash
command, run:
$ man bash
Now, type /echo
and press n
to until you see the help section of echo
command.
Alternatively, you can use the following one-liner command to display the help section of echo
command:
$ man bash | less --pattern='^ *echo +\['
Structure of the echo command
The echo
command accepts three optional arguments and the text to be printed to stdout.
echo [-neE] [arg ...] -n → Do not append a new line -e → enable interpretation of backslash escapes -E → disable the interpretation of backslash escapes (default)
Let’s skip the three optional arguments for now and see how to use echo
to print a text to the stdout (Terminal).
You can either run the following piece of code directly in the terminal or create a script file and then run it. I am going to run it directly in the Terminal:
$ echo "Hello World" Hello World
Take a look at the above example. It is the traditional hello world
program that we run whenever we learn any new language and run it as the first code. The echo
command here accepts "hello world"
text as an argument and prints it to the terminal (Stdout).
How to suppress new line character
The default behavior of the bash echo
command is, it will automatically add a new line (\n
). Run the same hello world
program again and notice the terminal prompt will be displayed in a new line.
$ echo "Hello world" Hello world
Now if you wish to suppress this behavior, you can use the "-n"
flag which will suppress the new line and the terminal prompt will be printed in the same line as shown below.
$ echo -n "Hello world" Hello world
What happens when no argument is passed
When no argument is passed to the echo command, the command will not fail but will return an empty new line. Launch your terminal and run "echo"
and you will see it just prints an empty line and exits.
$ echo
$ echo $?
0
You can override this behavior by adding the "-n"
flag which will not print a newline even if no argument is passed.
$ echo -n
Where will this be useful? You might wonder. When you want some empty lines between your outputs, you can use echo
without arguments.
Working with variables and commands
The echo
command will interpret user-defined or shell variables that are passed as an argument and print the value. Take a look at the below examples.
First, the env variable "SHELL"
is passed to the echo
command and it prints the value of the variable.
# Environmental Variable: $ echo " I am currently using ${SHELL}" I am currently using /bin/bash
Secondly, the user-defined variable "NAME"
is passed to the echo
command and the value is printed to the terminal.
# User Defined Variable: $ NAME="PoP_OS" $ echo "I am using ${NAME}" I am using PoP_OS
You can also run commands inside the arguments. Take a look at the below example where the "uname -r"
command is passed as an argument and the command is interpreted and the output is printed.
$ echo "KERNEL_VERSION=$(uname -r)" KERNEL_VERSION=5.11.0-7620-generic
Impact of using single and double quotes
In all the previous examples, you might have noticed that I have been using double quotes around the arguments. So what happens when the argument is not enclosed with quotes? Echo Command will not fail and you will get the same output.
Have a look at the following example. I am running the same command which I used in the previous section but without quotes and it prints the output without any error.
$ echo I am using ${NAME} I am using Pop_OS
As a good practice, always enclose the arguments with quotes.
If the argument is the plain text, you can use either single or double quotes. But if you wish to run any command or expand any variable in the argument, you should use double quotes, not single quotes.
The single quote will prevent any command to run or variable to be expanded. It will just treat the entire argument as plain text.
Look at the below example. When I enclose the argument with single quotes, the variable "NAME"
is not interpreted and treated as text.
# SINGLE QUOTES $ echo 'I am using ${NAME}' I am using ${NAME} # DOUBLE QUOTES $ echo "I am using Linux ${NAME}" I am using Linux PoP_OS
Output redirection and piping
As stated earlier, the echo
command prints the output to the standard output (terminal) by default. When you write shell scripts, you may end up in a situation to store the output of the echo
command to a file. You can do this using the bash redirection operator.
$ echo "Welcome to OSTechNix" > /tmp/op.log
$ cat /tmp/op.log Welcome to OSTechNix
You can also send the output of the echo
command to the pipe
for further processing.
$ echo "I am using ${NAME}" I am using PoP_OS
$ echo "I am using ${NAME}" | grep -i -o POP PoP
Multiline string
Your argument may sometimes spawn to more than one line. You can enclose the multiline argument with double quotes to print the output considering new lines, tabs, and spaces.
$ echo "This is Line 1
This is Line 2
This is Line 3"
In some cases, though your argument is passed as a multiline string, you may wish to print it as a single line. In such cases, use the "\"
symbol at the end of each line, so the next line will be in continuation with the previous line.
$ echo "I am running \ the echo command \ again"
Sample output:
I am running the echo command again
This is not exclusively specific to the bash echo
command. You can use this approach to spawn very large commands to multiline for better readability.
Backslash escapes interpretation
The echo
command accepts many escaped characters as arguments. For these special characters to be recognized by the echo command, the "-e"
flag should be passed. You can get the list of supported special characters from the echo
command's man page.
You will not be using all the special characters in your day-to-day life. You will mostly use newline characters, tabs, and backslash escape characters.
New line character - \n
A new line will be added when you pass \n
to the echo
command. You can also pass multiple \n which will add more lines accordingly.
$ echo -e "This is the first line\nThis is the second Line\nThis is the third line"
Sample output:
This is the first line This is the second Line This is the third line
Horizontal tab - \t
When \t
is used, it will add tab spaces. You can also combine multiple escape characters like as I did below.
$ echo -e "Enter the U_NAME\t:\n" Enter the U_NAME :
Vertical tab - \v
To create tabs vertically, use \v
.
$ echo -e "Enter the username\vEnter the password" Enter the username Enter the password
Suppress output - \c
When \c
is used, anything that comes after \c
will be suppressed.
$ echo -e "I am using \cCosmic Desktop" I am using
Carriage return - \r
When \r
is used, the cursor is reset to the first character and whatever comes after \r
is printed replacing existing characters.
$ echo -e "I use Arch, \rBtw" Btwse Arch,
Disable backslash escapes interpretation
The default behavior of echo
command is it will treat special characters as plain text unless -e
flag is passed. The same behavior can be achieved by using the -E
flag.
I will show you an example.
Let me run echo
command without any flags:
$ echo "Welcome to OSTechNix \nblog" Welcome to OSTechNix \nblog
Next, I run the same command with -E
Flag:
$ echo -E "Welcome to OSTechNix \nblog" Welcome to OSTechNix \nblog
As you can see, the result is similar when I ran the echo
command with -E
flag and without any flag. In both cases, echo
command treats the /n
special character as plain string.
Now let me run the same command again, but this time with -e
flag.
$ echo -e "Welcome to OSTechNix \nblog"
Welcome to OSTechNix
blog
Did you spot the difference? The echo command didn't treat /n
character as plain text and it displays whatever comes after the /n
special character in the new line.
Therefore, if you want to use any special characters like \n
, \t
, \v
etc., you should use -e
flag. Otherwise echo
will treat everything as plain string.
Allow me to show you another example.
$ echo -e "Hello world\nHello World\nHello World" Hello world Hello World Hello World
As you see in the above output, echo
command displays "Hello World"
string three times, one by one (i.e. in new line).
What if you want to escape only a specific special character? This is where double backslash (\\
) comes in rescue.
For instance, I want to escape the third /n
special character in the previous command.
To do so, I am going to prefix the third special character with double backslash(\\
) like below:
$ echo -e "Hello world\nHello World\\\nHello World"
Hello world
Hello World\nHello World
Now, echo
command treats the third \n
special character as plain string.
Therefore, when you have a lot of special characters and you wish to escape only a specific special character, you need to add the double backslash (\\
) in front of that special character.
Conclusion
In this article, we have seen what echo
command is and different ways to use it. This is a very simple command and there will be no shell script without using echo
command unless you decide to use printf
which is the alternative for echo
command in bash.