Using echo Command in Linux

Using echo Command in Linux with Practical Examples

As a pre-installed command in Linux, echo is a feature for printing out arguments as standard output. Using the echo Command in Linux with Practical Examples is what you will learn in this article. The echo command is one of the most frequently used commands in Linux that allows you to display messages as command results or text strings. As a user, you can use this command to display the text string that you have provided. The echo command is a widely used built-in command for Linux bash and C shells.

To let this command function as it’s designed, two conditions must be considered:

  • Have access to the command line.
  • Be used on an Operating System that runs on Linux.

So, to purchase your preferred Linux VPS, review some cheap and reliable plans.

Echo Command in Linux with Practical Examples

Before going through the practical examples of the echo command, let’s get more familiar with this command and see what the Syntax of echo looks like.

The syntax for the echo command is something like you see below:

echo [option(s)] [string(s)]

According to the above Syntax, if you decide to print ”Hello World!’’, you need to run:

echo Hello World!

As you see, since there will be no changes, you will get the provided string as output when using echo without any printed option.

Using echo command in Linux; Considerable Tips

When using the echo command, consider the below points.

  1. The shell will substitute all variables, wildcard matching, and special characters before passing the arguments to the echo command.
  2. Although not necessary, it is a good programming practice to enclose the arguments passed to echo in double or single quotes.
  3. When using single quotes '' the literal value of each character enclosed within the quotes will be preserved. Variables and commands will not be expanded.

Echo command options

If you use the command of /bin/echo --help a list of all the available echo command options will be displayed. Let’s see the options listed that the echo command uses in Unix utilizes. By default, the -E option disables the interpretation of the escape characters.

Have a look at the table below to better understand Using the echo Command in Linux. In the following of this guide, you will review some practical examples. Stay with us.

OptionDescription
-nDisplays that output has omitted the new line.
-EThis is the command's default option, and it works by disabling escape
-eIt makes it possible to interpret the characters below:
\\Displays the "\" (backslash) character.
\aAlert (bell) when the output is displayed.
\b Displays a backspace character.
\cRemoves any output that comes after the escape character
\e This creates the escape character equal to pressing "Esc."
\fDisplays a form feed character
\nThe character adds a new line to one's output.
\r Displays a carriage return.
\tGenerates the horizontal tab spaces.
\v Displays a vertical tab.
\NNNIt is a byte whose octal value is NNN.
\xHHThis is the byte whose hexadecimal value is HH.

Echo Practical Examples

Using the echo command might be easier if you see when you can use it. In this section, some practical examples are presented to help you understand how to use echo on your Linux system.

1. Examples of changing output formats

To incorporate escape characters, you can use the -e option. Using these characters, allows you to customize your echo command’s output easier.

For example, to shorten your output, you can use \c. To do this, you need to remove the section of the string which follows the escape character:

echo -e 'Hi World! \c This is Opera!' 

To make sure that all quotation marks would be interpreted accurately, enclose your string in single quotes while using the -e option.

Also, when you need to relocate this output to a new line, use \n:

echo -e 'Hi \nWorld, \nthis \nis \nOpera!'

Use \t to add horizontal tab space to your command:

echo -e 'Hi \tWorld!'

You can add \v to generate vertical tab spaces:

echo -e 'Hi \vWorld, \vthis \vis \vOpera!' 

If you are interested to change your foreground and background colors and apply to setting text properties like bold and underscore, you can use ANSI escape sequences. Let’s see what this command looks like:

echo -e '\033[0;30mBLACK' 

echo -e '\033[0;32mGREEN' 

echo -e '\033[1;37mWHITE' 

echo -e '\033[0;34mBLUE' 

echo -e '\033[0;31mRED'

echo command for changing output formats

2. Examples of Writing/redirecting to a file

To include or redirect the string in a file, you can use the > or >> operators instead of broadcasting output on the monitor.

sudo echo -e 'Hi World! \nThis is Opera!' >> test.txt 

The use of echo in this part is to create this specific file (if it does not already exist). You can also use the cat command to see the content of the file.

cat /tmp/file.txtCopyHi World! This is Opera!Copy

While the > character adds a new string to the already existing content, this character overwrites the text file’s content.

3. Example of displaying variable values

Another use of the echo command is to display variable values as the output. You can use it when you need to show the current user’s name.

echo $USER

4. Example of displaying command outputs

The echo command allows you to incorporate the result of other commands into the output.

echo "[string] $([command])

let’s see what this command means:

[string]: The string you want to use with the echo command.

[command]: The command you want to combine with echo to show the result.

5. Example of displaying a text line with a single quote

You can also use the echo command to print a single quote. You just need to use the ANSI-C Quoting or enclose it inside double quotation marks:

echo "I love Linux." Copyecho $' I \Love Linux.' CopyI love Linux.Copy

6. Example of displaying a line of text with a double quote 

In some situations, you might need to print a double quote. To do this, escape it with a backslash character or enclose it within single quotation marks:

echo 'Hi "OperaVPS"'Copyecho "Hi \"OperaVPS\""CopyHi "OperaVPS"Copy

7. Example of Pattern match characters

As a programmer, you may need to pattern-match characters. So, you can use the echo command as below to get all.html files currently in the directory.

echo The PHP files are: *.htmlCopyThe PHP files are: index.html contact.html functions.htmlCopy

8. Example of displaying variables

In the last echo example, you are learning how to use it for displaying variables. Use the command below to print the name of the current user.

echo $USERCopyOperaVPSCopy

The echo command is usually used by Shell scripts to display messages and output results from other commands. $USER plays the role of a shell variable used to hold your username.

Conclusion

In this article, Using echo Command in Linux with Practical Examples was explained to help you know when you can use the echo command as the most basic command in Linux. From now on, you know how to use the echo command in Linux. Also, as one of the most frequently used commands, the arguments passed over to this are printed onto the standard output. Share your experience if you have used any of the above commands.

Leave a Reply

Your email address will not be published. Required fields are marked.