What is Echo Command in Bash [With 3 Practical Examples]

The echo command is a built-in command in Bash scripting. Braiyan Fox and Chet Ramey developed it. Its primary purpose is to display text to the terminal. This article will discuss the use cases of echo command with practical examples.

Key Takeaways

  • Learning basics of the echo command.
  • Learning the use of escape characters in echo
  • Learning to change font & background colors with echo command.
  • Learning to create a timer using the echo command.

Free Downloads

Print Output Using Echo command

One can implement the echo command and its options using the following command structure.

Command Syntax >

echo [OPTION] [String]

Useful Options

There are a few options available in the echo command. Among them -n, -e, -E are most useful. These options can be employed in various ways to perform innovative tasks.

You can read this guide to learn more about the echo command in Linux.

3 Basic Examples of Echo Command in Bash

The basic use of echo commands involves the tasks of printing plain text, text with variables, and text with commands, etc. I will discuss all of the above one by one in the following examples.

Example 1: Display Plain Text Using Echo Command in Bash

The echo command is the default choice for printing any message in the terminal. And one can do it just by putting the text in a quote after the command(echo “Text”).

Steps to Follow >

❶ At first, launch an Ubuntu Terminal.

❷ Write the following command to open a plain_text.sh file in the build-in nano editor:

nano plain_text.sh

  • nano: Opens a file in the Nano text editor.
  • plain_text.sh: Name of the file.

Creating a .sh file in nano❸ Copy the following script and paste it into nano. Press CTRL+O and ENTER to save the file; CTRL+X to exit. Alternatively, copy the following script. Paste the script in a text editor and save it as .sh file.

# !bin/bash

echo “This is plain text. The program should print the text when running.”

This script prints the text given within the double quotes after the echo command.

❹ Use the following command to make the file executable:

chmod u+x plain_text.sh

  • chmod: Changes permissions.
  • u+x: Giving the owner executing permission.
  • plain_text.sh: Name of the script.

Changing execution permission❺ Run the script by the following command:


Display text using echo Bash commandOnce run the program it shows the text after the echo command.

Example 2: Printing Variable Using Echo Command in Bash

One can easily print a variable using the echo command. To print a variable using the echo command give a dollar sign (‘$’) before the variable name.

You can follow the Steps of Example 01 to learn about creating and saving shell scripts.

Scripts (variable.sh) >


echo "Enter a number:"
read num
echo "Given number is:$num"

This script takes input from the user and stores it in a variable. Later it echoed back the variable with a text using a dollar sign.
Display variable using echo Bash commandAt first, 10 is inserted by the user. Then the script recalls the number and prints ‘Given number is:10’

Example 3: Display Text with Command Substitution

One can substitute a command with the dollar sign and display the output with text using the echo command.

You can follow the Steps of Example 01 to learn about creating and saving shell scripts.

Scripts (command.sh) >


echo "Current directory:$(pwd)"
echo "The total number of files in this directory:$(ls | wc -l)"


The script will print the current directory as well as the number of total files in the current directory.

Display text with command outputHere, the echo command is used to display the text and output of the command. The image shows that the output of the  ‘ls | wc -l’ command is ‘50’. This output is shown after the text “The total number of files in this directory:”.

Escape Characters in Bash with Echo Command

In Bash scripting, escape characters are used to perform various special functions such as inserting special characters, controlling formatting, or changing the behavior of certain commands. Here are some commonly used escape characters with the echo command in Bash:

Character Description Example code Output
\\ Printing Backslash echo “\\” \
\a Alert echo -e “\a” Give an alert
\b backspace echo -e “World\b\b\bd” Wod
\c Produce no further output echo -e “Hello, World! \cHow are you?” Hello, World!
\e Escape echo -e “\ehello” ello
\f Form Feed echo -e “This is some text.\fThis is after the form feed.” This is some text.
This is after the form feed.
\n Newline echo -e “Line 1\nSecond line\nLine 3” Line 1
Second line
Line 3
\r Carriage return echo -e “Loading……Done\rProgress: 50%” Progress: 50%Done
\t Horizontal Tab echo -e “Name:\tJim\tAge:\t30” Name:   Jim    Age:    30
\v Vertical Tab echo -e “Name:Jim\vAge:30” Name:Jim
\0NNN Byte with octal value NNN(1 to 3 digits) echo -e “Loading..60\0045” Loading..60%
\xHH Byte with hexadecimal value HH(1 to 2 digits) echo -e “Hello, World\x21\x21” Hello, World!!

Note: All these characters come into action when the command option -e is enabled.

Change Font and Background Color with Echo

One can even change the font and background color while using the echo command. The following list contains a few escape characters that are used to change font and background color.

Character Description Example code Output
\033[0m Reset all attributes
\033[1m Bold echo -e “\033[1mThis is bold text\033[0m” This is bold text
\033[4m Underline echo -e “\033[4mThis text is underlined\033[0m” This text is underlined
\033[5m Flashing “\033[5mFlashing Text\033[0m” Flashing Text
\033[31m Red text echo -e “\033[31mRed Text\033[0m” Red Text

Create a Timer Using Echo Command in Bash Script

The best way to visualize the echo command and its functionality is by creating a timer. So, open a file named ‘timer.sh’ & write the following script in the nano editor.

You can follow the Steps of Example 01 to learn about creating and saving shell scripts.

Scripts (timer.sh) >


echo "Timer started."

while [ $seconds -gt 0 ]; do

if [ $seconds -eq 3 ]; then
echo -ne "Countdown: \033[1;32m$seconds\e[0m\r"  # Green color
elif [ $seconds -eq 2 ]; then
echo -ne "Countdown: \033[1;33m$seconds\e[0m\r"  # Yellow color
echo -ne "Countdown: \033[1;31m$seconds\e[0m\r"  # Red color

sleep 1

echo -ne "\033[5;
1;31mTime's up!\e[0m\n"  # Red color


The script starts by printing “Timer started.” to the terminal using the echo command. It initializes a variable ‘seconds’ with a value of ‘3’. It enters a while loop that executes as long as seconds is greater than 0.

Inside the loop, there are three ‘if’ conditions to determine the color of the countdown text based on the value of seconds. The escape sequences \033[1;32m, \033[1;33m and \033[1;31m set the text color to green, yellow, and red respectively. The escape sequence \e[0m resets the text color to the default.

After printing each countdown value, the script pauses for 1 second using sleep 1 to create the countdown effect. It decrements the value of seconds by 1 using the ((seconds–)) construct.

Once the loop finishes and seconds become 0, it prints “Time’s up!” in red color using the escape sequence \033[5;1;31m for blinking red, bold text, and \e[0m to reset the text color.

Finally,  run the script using the following command:


Creating a timer using echo Bash commandIn the output provided, you can observe the countdown timer dynamically changing its color. It starts off as green, transitions to yellow, and eventually turns red. When the countdown reaches zero, the script prints “Time’s up!” in red color. The text is displayed in bold and blinks to draw attention to the completion of the countdown.


In conclusion, the echo command has various options and escape characters to create useful programs. I believe now you have a better understanding of the functionality of the echo command.

People Also Ask

How to echo user input?
User inputs are usually stored in a variable. Just give a dollar($) sign before the variable name to print user input. This will retrieve the value stored in the variable and can be used as parameters or arguments of other commands.
What is the difference between printing a variable and substituting a command using echo?
The main difference is one must put a dollar sign[&variable_name] before the variable name to print a variable. On the other hand, substituting a command requires a first bracket[$(command_name)] along with a dollar sign
How do I set variables in bash?
Is Bash echo command is same as python print? “The Bash echo command is more or less equivalent to the Python print function. However, the Python print function is more versatile and can handle multiple arguments.

Related Articles

<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial

Rate this post
Md Zahidul Islam Laku

Hey, I'm Zahidul Islam Laku currently working as a Linux Content Developer Executive at SOFTEKO. I completed my graduation from Bangladesh University of Engineering and Technology (BUET). I write articles on a variety of tech topics including Linux. Learning and writing on Linux is nothing but fun as it gives me more power on my machine. What can be more efficient than interacting with the Operating System without Graphical User Interface! Read Full Bio

Leave a Comment