Echo Command in Linux

What is the echo command in Linux? The echo command comes pre-installed in Linux, and it is a feature for printing out arguments as standard output.

Updated: 05 Mar, 23 by Susith Nonis 7 Min

List of content you will read in this article:

What is the echo command in Linux? The echo command comes pre-installed in Linux, and it is a feature for printing out arguments as standard output. This command is typically used to display messages as command results or text strings. Echo typically displays text strings provided by the user. For this command to function as designed, it must: 

  • Have access to the command line 
  • Be used on an operating system that runs on Linux.

echo [option] [string] 

For example, below is the command you need to print to get 'Hi Friend!' as your output: 

echo Hi Friend! 

You should note that you'll get the provided string as output when using echo without any printed option, as there won't be any changes. 

There are some points that you need to consider when using this command. 

  1. The shell substitutes all special characters, variables, and wildcard matching before passing the arguments to the echo command. 
  2. When you use single quotes, the value of every character enclosed within these quotes remains preserved. The commands and variables will at no point become expanded. 
  3. Although it is not mandatory, it is a good practice to encase the arguments that you pass on to the echo command in single or double quotes as a programmer. 

If you want to view a list of all the available echo command options, you should use --help. Your command will hence be:

/bin/echo --help

Remember that the command echo --help will bring back --help. 

The echo command in Unix utilizes the options listed below. 

  • -n: This option shows the output having omitted the new line.
  • -E: This is the command's default option, and it works by disabling escape characters' interpretation. 
  • -e: It makes it possible to interpret the characters below:
  • \\: Displays the "\" (backslash) character.   
  • \a: Provides a sound alert bell when the output is displayed. 
  • \b: Creates the backspace character, equal to pressing the "Backspace" button.
  • \c: Removes any output that comes after the escape character 
  • \e: This creates the escape character equal to pressing "Esc." 
  • \f: This represents the form feed character. It automatically causes the printer to move to the beginning of the next page. 
  • \n: The character adds a new line to one's output.
  • \r: It conducts a carriage return.
  • \t: Generates the horizontal tab spaces.
  • \v: Generates the vertical tab spaces.
  • \NNN: It is a byte whose octal value is NNN.
  • \xHH: This is the byte whose hexadecimal value is HH

Here are examples of how you can use echo on your Linux system. 

1.   To change output formats 

When you use the -e option, you'll be able to incorporate escape characters. These characters make it easier for you to customize your echo command's output. 

For example, you can use \c to shorten your output by removing the section of the string which follows the escape character:

echo -e 'Hi Friend! \c This is KPAN!' 

NB: If you choose to go with the -e option, remember to enclose your string in single quotes. It will ensure that any present quotation marks get interpreted accurately. 

Use \n whenever you intend to relocate this output to a new line:  

echo -e 'Hi \nFriend, \nthis \nis \nKPAN!'

You can add horizontal tab space to your command using \t

echo -e 'Hi \tFriend!'

Add \v to generate vertical tab spaces:

echo -e 'Hi \vFriend, \vthis \vis \vKPAN!' 

If you want to change your foreground and background colors, use the ANSI escape sequences. This also applies to setting text properties like bold and underscore. The command will be as follows: 

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' 

2.   Writing/redirecting to a file 

Instead of broadcasting output on the monitor, you can use the > or >> operators to include or redirect this string in a file: 

sudo echo -e 'Hi Friend! \nThis is KPAN!' >> test.txt 

If this specific file does not already exist, the echo command will create it. To see the contents of the file, use the cat command:

cat /tmp/file.txtCopyHi Friend! This is KPAN!Copy

Note: Using the > character overwrites the text file's content while >> adds a new string to the already existing content.

3.   Display variable values 

 You can also use the echo command to display variable values as the output. For instance, if you want to show the current user's name, use: 

echo $USER

4.   Display command outputs 

Using the echo command, you can incorporate the result of other commands into the output using:

echo "[string] $([command]) 

In this command: 

  • [string]: This represents the string you intend to use with the echo command. 
  • [command]: This is the command you intend to combine with echo to show the result. 

5.   Displaying a line of text with a double quote 

If you wish to print a double quote, do so by escaping it with a backslash character or enclosing it within single quotation marks:

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

6.   Pattern match characters

Programmers can use the echo command with pattern matching characters like wildcards. For instance, you will get all .html files currently in the directory using the command below:

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

7.   Displaying a text line with a single quote

If you want to print a single quote, you should use the ANSI-C Quoting or enclose it inside double quotation marks: 

echo "I'm a Unix fan." Copyecho $' I \'m a Unix fan.' CopyI'm a Unix fan.Copy 

8.   Display variables

The echo command can be used to display variables. The following example demonstrates how to print the name of the current user: 

echo $USERCopylinuxizeCopy

$USER is the shell variable used to hold your username.

The echo command in Linus is among the most basic commands, and it is also one of the most frequently used. The arguments passed over to this command are printed onto the standard output. Shell scripts often utilize echo to display messages and output results from other commands. The samples and descriptions provided above should help you better understand what this command is all about. 

People also read: 

Susith Nonis

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.