How to Use echo Command in Linux

When it comes to popularity and utility, the echo command in Linux is regarded as one of the most flexible tools. It is one of the most commonly used commands in shell scripting while also being one of the easiest to learn. Let’s dive in and learn echo and its practical uses with the Linux environment.

What is the echo command in Linux? 

The echo command in Linux simply shows strings as standard output in the terminal. It is a built-in command in Bash and is available in a multitude of operating systems. Echo functions similarly, just like printf from most programming languages. 

Primarily, it is used to show output and processes while or after executing a script. But you can also use it when creating your bash aliases to customize your command line experience or to automate some of the processes. 

You can still make and run scripts without echo, but troubleshooting and monitoring the status of the script will be a pain in the neck. 

Default echo Syntax

For the echo command to work properly, the user will need to insert strings or make a command do so automation. You can use pre-determined text or manual inputs to make the command work. However, since it displays output in the terminal, you can only use strings or texts.

$ echo Your Text Here  or $ echo “Your Text Here”

Another way to use echo is to display a pre-determined text, which is done by putting strings inside open and closing double apostrophes.

The output will stay the same regardless of how many times you run the command unless you add the -e command, which will be discussed below.

Relevant Options for echo Command in Linux

There are only three main options for the echo Command. Within these three options, only -n and -e can be used productively. The -e option’s function is to interpret what is called “backslash-escaped characters,” which are utilized to format how the texts are displayed in the terminal.

-n: Does not allow newlines

-e: Checks, interprets and displays backslash-escaped characters

-E: Disables the backslash-escaped sequences

Backslash-escaped characters can format text and add some functionalities that can be useful when running scripts. Here are some of the most useful backslash-escaped characters for the echo command.

\b: Inserts a backspace character

\c: Only shows texts before this character

\r: Only shows texts after this character

\e: Executes the exit character

\n: Displays a newline

\t: Shows a horizontal tab (functions like spacing)

\v: Prints a vertical tab (functions like regular tab)

\a: Creates an alert sound upon execution

Use the format below to use a backslash-escaped character (replace the N any letters mentioned above):

$ echo -e “ Text \N text.”

Example Uses of echo Command in Linux

There are three commonly-used methods to incorporate echo command in scripts: displaying texts, variable value, and command output. However, you can also use the command to create and edit new files similarly to ls. You can mix and match these methods to customize the output’s appearance in the terminal.

Displaying Texts

Everything that follows echo in the command line is printed as output. However, if you need to use it on a scripting project as a pre-determined text, enclose your input inside double apostrophes.

$ echo “Text here”
echo Command in Linux

Displaying Variable Value

What if you want to print a variable that might or might not change value? In this case, your best option is to use $ just like displayed below. In this example, I created a custom variable named Mumbotato and set the value to the string “CEO of Boatem.” This method also makes it easier to insert common phrases that can take a while to type.

$ echo $variable
echo Command in Linux with variable

Note: echo will not show anything if the variable’s value is undefined. Store your custom variables and commands in the bash aliases files to continue using it even after reboot. Variables defined in the command line will disappear after closing your terminal window.

Displaying Outputs from Commands

Just like variables, you can display the output of a command using $, but this time, you need to define the command so echo can execute it and print the result. For instance, in the example below, we insert a df command showing all the remaining space of the ext4 filesystem.

$ echo -e “Text here $(insert command here)”
echo with other commands

You can also see how the \n backlash-escaped character affects the output of the df command (it displays the actual output on a new line). Echo can output the result from other commands as long as it is in text form.

Displaying Outputs with Sound

If you want to be notified when a certain event is happening while executing a script, your best option is to run the \a backslash-escaped character. When \a is implemented, it will execute a sound including the text pre-programmed alongside it. Don’t forget to increase your volume because it can be easy to miss sometimes.

$ echo “Text \a text.”
echo Command in Linux with sound

Displaying Specific Files

You can also list files based on their extensions using the echo command, which might be helpful if you want to use ls in your script output. To do so, just type the asterisk sign followed by the file type that you want to show (including the dot.)

$echo *<file extension>
echo Command showing file lists

Creating and Editing Text Files

Another thing that you can do with echo is to save its output in a text file. Just like other commands like xargs, you can “funnel” the content using the > operand and provide the filename that you want to store the output into.

$ echo “Text” > filename.txt
using echo as ls

Final Thoughts

Learning echo is easy and intuitive. But the real challenge is how to innovate around it and use it to improve your scripting skills. Hopefully, this guide helped you grasp the basic functions and usage of the echo command. For more Linux guides, check out the how-to’s section.

If this guide helped you, please share it. 🙂

Total
0
Shares
Leave a Reply

Your email address will not be published.

Previous Article

How to Use diff Command in Linux

Next Article

How to Use top Command on Linux

Related Posts