How to Use echo Command in Bash Scripts in Linux

echo is a shell built-in command that is used to print the information/message to your terminal. It is the most popular command that is available in most Linux distributions and is typically used in bash scripts and batch files to print status text/string to the screen or a file.

In this article, I will show you how to use the echo command in Linux shell scripts.

How to Work With echo Command in Linux

When learning shell scripting this is the first command you will learn about to print something in your terminal. echo will print the output to stdout or you can also redirect the output to files. There are two versions of echo. One is bash builtin and the second one is an external command.

NOTE: Always builtin version takes precedence over external command.

Use the type command to get the path information about the echo command.

$ type -a echo

echo is a shell builtin
echo is /usr/bin/echo
echo is /bin/echo

To get the list of options supported for the echo command, use the help option.

$ help echo

Example 1: No Arguments to echo Command

When you call the echo command without passing any arguments it prints an empty line.

$ echo
echo with no arguments
an echo with no arguments

Example 2: Use echo With and Without Quotes

You can pass arguments to echo command with or without quotes. Take a look at the below example. I am printing the same statement with single and double quotes and without quotes and getting the same result.

$ echo Love Linux And OpenSource
$ echo "Love Linux And OpenSource"
$ echo 'Love Linux And OpenSource'
echo print output
echo print output

There is a significant difference in when to use single and double quotes. When you use single quotes and any word contains a single quote then it will be treated as the end of quotes. Take a look at the below example where isn’t contains single quotes and is treated as the end of quotes.

$ echo 'Windows isn't opensource'
> ^C

In this case, using double quotes will make more sense.

$ echo "Windows isn't opensource"
Windows isn't opensource
Echo Single Vs Double Quotes
Echo Single Vs Double Quotes

Example 3: Printing Variables with Echo Command

Values stored in variables can be printed to the terminal using the echo command. When you use single quotes variable ${VAR} will be interpreted as text and will not be expanded to its assigned value (welcome).

$ VAR="Welcome"
$ echo ${VAR} to shell tips
$ echo "${VAR} to shell tips"
$ echo '${VAR} to shell tips'
Echo Printing Variable Value
Echo Printing Variable Value

Example 4: Redirecting Output to File

You can redirect output to a file instead of printing it in a terminal using the redirection operator ( > and >>).

$ echo " Writing to file redirect.txt " > redirect.txt
$ echo " Appending to file redirect.txt " >> redirect.txt
  • Using > operator will create a new file if not exists and write the data and if the file exists it will overwrite the data.
  • Using >> operator will create a new file if not exists and append the data if the file exists.
Redirecting Output to File
Redirecting Output to File

Example 5: Supported Arguments

echo command supports three arguments.

Echo Arguments
Echo Arguments

By default when you run the echo command new line character is automatically appended at the end. If you want to suppress this behavior use -n flag.

$ echo -n "Love Linux And OpenSource"
Suppress New Line Character
Suppress New Line Character

By using the -E flag, the echo statement will treat all backslash-escaped characters as plain text. It is not mandatory to use -E flag because echo by default will not treat these as special characters.

$ echo "\nLove Linux And OpenSource"
$ echo -E "\nLove Linux And OpenSource"
Escaped Characters
Escaped Characters

To use backslash-escaped characters you have to use -e flag.

Example 6: Escaped Characters

echo command supports escape characters like newline, tab, vertical tab, and a few more. You can get the list of supported characters from the help command.

$ help echo
Escaped Characters
Escaped Characters

Let’s see frequently used escape characters in action. Note you have to use -e argument for any escape characters to be effective.

Newline character(\n) – This will be one of the commonly used escape characters. When you use \n it will add new lines.

$ echo -e "\nWelcome to \nLinuxShellTips"

Welcome to 
LinuxShellTips

Horizontal tab(\t) – To create horizontal tabs use \t which adds a single tab in your statement.

$ echo -e "\tWelcome to \tLinuxShellTips"
	Welcome to 	LinuxShellTips

Vertical tab(\v) – To create vertical tabs use \v.

$ echo -e "\vWelcome \vto \vLinuxShellTips"

Welcome 
        to 
           LinuxShellTips

Carriage Return(\r) – Removes everything that comes before \r and prints only what is after \r.

$ echo -e "Welcome to \r LinuxShellTips" 
 LinuxShellTips

Suppress Further Output(\c) – Carriage return removes everything that comes before it and \c will remove everything that comes after it.

$ echo -e "Welcome to \c LinuxShellTips"
Welcome to 

If you want to treat any escape characters as normal characters you can use \\. Let’s say you want \c to be treated as a normal value then use \\ followed by escape character \c.

echo -e "Welcome to \\\c LinuxShellTips"
Welcome to \c LinuxShellTips

That’s it for this article. The echo is a simple command to use and very easy to learn. We will catch you up with more shell script-related articles soon.

1 thought on “How to Use echo Command in Bash Scripts in Linux”

Got something to say? Join the discussion.