The “echo” Command in Linux [7 Practical Examples]

The echo command in Linux is a frequently used command. This command in general shows a text or line in the command line. But there are others uses of this command as well. You can pipe or redirect the echo command with another command to do certain things like create/edit files if you want.

A. Description

The echo command in Linux takes a string of text and displays the output in the command line. At first glance, this command may look like a straightforward command, but there are many uses for this command. Especially, when redirecting with a file you can edit or create a new file. You also have the option of creating great customization in the text and adding commands and variables.

B. Syntax

The syntax of the echo command is really simple. You just add your text after the echo command and the command will output everything after the command in the command line.

echo [SHORT-OPTION]... [STRING]...
echo LONG-OPTION

Note: You can also keep the text inside double or single quotes. By default, the system takes no quotes as double quotes. But in case you use single quotes, the command line will print exact text inside without considering any variables or commands inside.

C. Options

The echo command has several options that you can use. Here are some of the available for the echo command. You can view the options page using the man command.

man echo

Useful Options

  • -n (Do not output the trailing new line)
  • -e (Enable interpretation of the backslash escapes)
  • -E (Disable interpretation of the backslash escapes)
  • –help (Display the help section)

Note: The Options are case-sensitive. So, be careful about it.

Practical Examples of the “echo” Command in Linux

The echo command in Linux has many practical uses. But the primary function is to output some text in the command line. Here are some of the practical functions of the echo command.

Example 1: Using the “echo” Command to Display Text in the Command Line

You can use the echo command to display text in the command line. The syntax is quite simple. Just type echo and text after the command.

echo TEXT

You can follow the steps below to view any text in the command line using the echo command.

Steps to Follow >

➊ At first open the Terminal in the Home directory.

➋ Type the following command in the command prompt:

echo LinuxSimply All About Linux

➌ Now, press the ENTER key.

Output >

As you can see in the command line, the output is just the text typed after the echo command.

echo command in linux text output

Example 2: Displaying Commands in the Command Line Using the “echo” Command

You can also display any command inside the echo command. All you need to do is type the dollar sign ($) and keep the command inside the parentheses. The general syntax is as follows.

echo TEXT $(COMMAND)

You can try this example by following the steps below.

Steps to Follow >

➊ At first open the Terminal in the Home directory.

➋ Change the current directory to the Desktop directory using the following command.

cd Desktop/

❸ Type the following command in the command line.

echo The Files Inside the Desktop Are: $(ls)

❹ Now, press the ENTER key.

Output >

As you can see, the file names on the Desktop are shown after the text.

ls command inside the echo command

Example 3: Using the “echo” Command to View Variables in the Command Line

In the command line, you can show different variables using the echo command. The general syntax is similar to using commands, just without the parentheses.

echo TEXT $VARIABLE

Suppose you want to view the system user variable and another custom variable. You can follow the steps below to try it out.

Steps to Follow >

➊ At first open the Terminal in the Home directory.

➋ Type the following command in the command line to view the system variable USER.

echo The User of the System: $USER

❸ Now you can declare a custom variable x by the following command.

x=10

❹ Now type the following command to view the custom variable.

echo The value of x is: $x

❺ Now, press the ENTER key.

Output >

As you can see in the image below, the system variable and the custom variable are shown in the output at the command line.

Showing text with variables

Example 4: Use of Double and Single Quotations in the “echo”  Command

You can use double or single quotations for different purposes for the echo command. In the case of a double quotation, you can use some commands or variables and show them in the output. But in the case of a single quotation, you will find only the exact text inside the quotations. You won’t be able to run variables or commands inside a single quotation.

Note: By default using no quotation is taken as a double quotation in the system. So you will be able to run commands and variables without any quotations.

You can try this out using the following steps.

Steps to Follow >

➊ At first open the Terminal in the Home directory.

➋ Type the following command in the command line to try double quotation.

echo “The Date in the User $USER is: $(date)”

Or,

echo The Date in the User $USER is: $(date)

❸ Type the following command in the command line to try a single quotation.

echo 'The Date in the User $USER is: $(date)'

❹ Now, press the ENTER key.

Output >

Here you will find that the double quotation lets the command line run the variable and command. But in the case of a single quotation, the output shows the exact text inside.

use of quotation



Example 5: Using the “echo” Command to Create a File with Text Inside

You can write some text and create a file with it by redirecting the echo command. The general syntax to do that is as follows.

echo TEXT>File.txt

Let’s say, you want to create a new file named LS.txt and add some text inside. You can follow the steps below to try this.

Steps to Follow >

➊ At first open the Terminal in the Home directory. You can also navigate to some other directory if you want.

➋ Type the following command in the command line.

echo This is LinuxSimply, All About Linux > LS.txt

❸ Type the following command in the command line to confirm the new file.

cat LS.txt

❹ Now, press the ENTER key.

Output >

As you can see the LS.txt file was created and the content was also there.

creating a new file and adding text

Example 6: Adding a Line at the End of a File Using the “echo” Command

You can also add a line at the end of a file using the echo command. The general syntax is quite similar to the previous example. But in case of redirecting, you need to append instead of writing a new file. The general syntax is shown below.

echo TEXT>>File.txt

Suppose, you already have a file named LS.txt and you want to add a line at the end of the file. You can follow the steps below to do that.

Steps to Follow >

➊ At first open the Terminal in the Home directory. You need to navigate to the directory where the LS.txt file resides. In this case, the LS.txt file is already in the Home directory.

➋ Type the following command in the command line to check the content of the LS.txt.

cat LS.txt

❸ Type the following command in the command line to write a new line in the LS.txt file.

echo Welcome, Everyone.>>LS.txt

❹ Now, you can check the content of the LS.txt file by using the cat command.

cat LS.txt

❺ Now, press the ENTER key.

Output >

As you can see the new line is now added to the end of the LS.txt file.

adding a new line to a file

Example 7: Customize/Formatting Text Using the “echo” Command

You can customize/format your text by adding, removing space, or creating a new line using the -e option with the echo command. There are many different sequences are available for the -e option for the echo command.

  • \\ (backslash)
  • \a (alert (BEL))
  • \b (backspace)
  • \c (produce no further output)
  • \e (escape)
  • \f (form feed)
  • \n (new line)
  • \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: You need to use either a single quotation or a double quotation for the text for formatting.

You can try it out using the steps below.

Steps to Follow >

➊ At first open the Terminal in the Home directory.

➋ Type one of the following commands in the command line.

echo -e "Welcome, \tEveryone.\nThis is \v LinuxSimply"
echo -e 'Welcome, \tEveryone.\nThis is \v LinuxSimply'

❸ Now, press the ENTER key.

Output >

echo command for formatiing

Conclusion

In this article, you’ve learned about different ways to use the echo command in Linux for many practical purposes. This command is very interesting as it shows many types of use for only a single command. You can practice this command extensively as it will provide greater flexibility and functionality in your arsenal as a professional user of Linux.


Related Articles

5/5 - (11 votes)
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Md. Rafsan Zani

Hello, I’m Md. Rafsan Zani. I have recently completed my Undergraduate from the Bangladesh University of Engineering and Technology (BUET). Currently, I’m pursuing higher studies abroad. I’m really interested in computer science and would like to learn a lot about the wonderful world of computers. Currently, I’m working as a Linux Content Developer Executive and find Linux really interesting. I certainly would like to learn more about Linux and implement them in my future studies. Read Full Bio

Leave a Comment