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.
- 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.
Print Output Using Echo command
One can implement the echo command and its options using the following command structure.
Command Syntax >
echo [OPTION] [String]
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.
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 >
❷ Write the following command to open a plain_text.sh file in the build-in nano editor:
❸ 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.”
❹ Use the following command to make the file executable:
chmod u+x plain_text.sh
❺ Run the script by the following command:
Once 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.
Scripts (variable.sh) >
#!bin/bash echo "Enter a number:" read num echo "Given number is:$num"
At 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.
Scripts (command.sh) >
#!bin/bash echo "Current directory:$(pwd)" echo "The total number of files in this directory:$(ls | wc -l)"
Here, 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:
echo -e “\a”
Give an alert
echo -e “World\b\b\bd”
Produce no further output
echo -e “Hello, World! \cHow are you?”
echo -e “\ehello”
echo -e “This is some text.\fThis is after the form feed.”
This is some text.
This is after the form feed.
echo -e “Line 1\nSecond line\nLine 3”
echo -e “Loading……Done\rProgress: 50%”
echo -e “Name:\tJim\tAge:\t30”
Name: Jim Age: 30
echo -e “Name:Jim\vAge:30”
Byte with octal value NNN(1 to 3 digits)
echo -e “Loading..60\0045”
Byte with hexadecimal value HH(1 to 2 digits)
echo -e “Hello, World\x21\x21”
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.
Reset all attributes
echo -e “\033[1mThis is bold text\033[0m”
This is bold text
echo -e “\033[4mThis text is underlined\033[0m”
This text is underlined
echo -e “\033[31mRed Text\033[0m”
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.
Scripts (timer.sh) >
#!/bin/bash echo "Timer started." seconds=3 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 else echo -ne "Countdown: \033[1;31m$seconds\e[0m\r" # Red color fi sleep 1 ((seconds--)) done echo -ne "\033[5; 1;31mTime's up!\e[0m\n" # Red color
Finally, run the script using the following command:
In 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 Print Output in Bash [With 6 Practical Examples]
- How to Echo New Line in Bash [6 Practical Cases]
- Cat Command in Bash [With 4 Practical Examples]
- How to Save Bash Output to File? [3 Practical Cases]
- How to Save Bash Output to Variable? [With Practical Cases]
- How to Set Command Output to Variable in Bash [2 Methods]
- How to Suppress Output in Bash [3 Cases With Examples]
- How to Change Color of Output Using Bash Script? [Easy Guide]