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.
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
❸ 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:
./plain_text.sh
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)"
The script will print the current directory as well as the number of total files in the current directory.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:
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
Age:30\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!!
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.
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
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:
./timer.sh
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.
Conclusion
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
Related Articles
- 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]
<< Go Back to Bash Output | Bash I/O | Bash Scripting Tutorial