What is “echo” Command in Bash [With 3 Practical Examples]

The echo is a shell built-in command in Bash used to display text or variables on the terminal. It is essential for various scripting and interactive tasks in the Unix/Linux command line environment. Understanding the nuances of the echo command empowers Bash users to streamline their scripts, improve readability, and effectively convey information within the command-line environment. Explore this article to learn the use cases of echo command with practical examples.

Syntax of “echo” Command

The syntax of echo command is:

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.

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

1. Print 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”. Here’s an example:

  1. At first, launch an Ubuntu Terminal.
  2. Create plain_text.sh file in the build-in nano editor:
    nano plain_text.sh

    Creating a .sh file in nano

  3. 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.

  4. Use the following command to make the script executable:
    chmod u+x plain_text.sh

    Changing execution permission

  5. Run the script by the following command:
    ./plain_text.sh

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

2. Printing Variable Using “echo” Command in Bash’

You 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. Here’s an example script to print variables using echo:

#!bin/bash

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

This script takes input from the user and stores it in num. Later it prints the variable with a text using a dollar sign with echo command.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’

3. Command Substitution in “echo” Command

You can substitute a command with the $ sign and display the output with text using the echo command. The below script uses command substitution inside echo command:

#!bin/bash

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

Here, $(ls | wc -l) is used inside echo to print the current directory as well as the number of total files in the current directory.Display text with command outputThe image shows that the output of the  ls | wc -l command is ‘50’.

What are the Escape Characters for “echo” Command?

Escape characters in Bash are special characters preceded by a backslash (\). They are used to represent characters that have special meanings to the shell, allowing you to include them in strings or commands without triggering their special behavior. Here are some commonly used escape characters with the echo command in Bash:

Character Description
\\ Printing Backslash
\a Alert
\b backspace
\c Produce no further output
\e Escape
\f Form Feed
\n Newline
\r Carriage return
\t Horizontal Tab
\v Vertical Tab
\0NNN Byte with octal value NNN(1 to 3 digits)
\xHH Byte with hexadecimal value HH(1 to 2 digits)
Note: All these characters come into action when the command option -e is enabled.

How to Change Font and Background Color With “echo”?

To change the font and background color, use echo command with the specific character code for the colour. 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

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

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 like $(command_name) along with a dollar sign.

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
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux

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