---
title: "Echo Command in Bash Shell : Discover the Many Uses"
description: "Learn about all the ways to utilize the echo command in Bash to create better scripts and master the Bash shell!"
canonical: "https://adamtheautomator.com/echo-command-in-bash/"
---

# Echo Command in Bash Shell : Discover the Many Uses

> Learn about all the ways to utilize the echo command in Bash to create better scripts and master the Bash shell!

Source: https://adamtheautomator.com/echo-command-in-bash/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Echo Command in Bash Shell : Discover the Many Uses](https://adamtheautomator.com/wp-content/uploads/2022/11/Echo-Command-in-Bash-Shell-Discover-the-Many-Uses.jpg)

# Echo Command in Bash Shell : Discover the Many Uses

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)18 November 20225 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Bash](/tag/bash/)[Linux](/tag/linux/)

Table of Contents

*   [Prerequisite](#prerequisite)
*   [Printing Strings with the echo Command in Bash Shell](#printing-strings-with-the-echo-command-in-bash-shell)
*   [Suppressing Trailing Newlines in Strings](#suppressing-trailing-newlines-in-strings)
*   [Printing Variable Values](#printing-variable-values)
*   [Interpreting Escape Sequences](#interpreting-escape-sequences)
*   [Inserting Strings to a New Line](#inserting-strings-to-a-new-line)
*   [Inserting Vertical Tabs Between Strings](#inserting-vertical-tabs-between-strings)
*   [Printing File Names Based on Extension](#printing-file-names-based-on-extension)
*   [Writing Output to File](#writing-output-to-file)
*   [Appending Output to Existing Files](#appending-output-to-existing-files)
*   [Conclusion](#conclusion)

As an admin, you may regularly run the echo command in Bash when managing a Linux system. But is printing texts all the echo command can offer?

In this tutorial, you will take a closer look at the echo command and learn some of its most useful practical examples.

Ready? Stay tuned and reshape your idea of the echo command!

## Prerequisite

This tutorial comprises hands-on demonstrations. To follow along, you only need an Ubuntu machine. This tutorial uses Ubuntu 20.04, but other Linux distributions will also work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

## Printing Strings with the echo Command in Bash Shell

When you think about the echo command, the first thing that comes to mind is printing string values to the console. Yes, the echo command returns strings directly from a command or script.

But later in this tutorial, you will see the echo command in a different view. For now, see how this command prints out strings to the console.

The syntax for the echo command is as follows where:

*   string – the text to print.
*   options – the optional arguments you can specify to modify the output.

```bash
echo string options
```

Run the below echo command without added options to print a simple text to the console. This command passes the string as an argument to the echo command.

```bash
echo "Echo command bash"
```

![Printing a string to the console](https://adamtheautomator.com/wp-content/uploads/2022/11/image-242.png)

Printing a string to the console

## Suppressing Trailing Newlines in Strings

The echo command adds a newline character at the end of each string by default. As a result, when you print multiple strings, perhaps connecting one to another, each string prints on a new line.

The -n option suppresses trailing newlines telling the echo command to print multiple strings on a single line. This option can be helpful, for example, when printing progress bars to the console.

Run the below command to print Progress: and 50%.

```bash
echo -n "Progress: " && echo "50%"
```

Below, you can see newlines separate the two strings.

![echo command bash : newlines separate the two strings](https://adamtheautomator.com/wp-content/uploads/2022/11/image-243.png)

echo command bash : newlines separate the two strings

Now, run the following command, appending the -n option, to print the strings Progress: and 50%.

```bash
echo -n "Progress: " && echo "50%"
```

As you can see below, both strings are now in a single line since you used the -n option to suppress the newlines.

![Suppressing trailing newlines](https://adamtheautomator.com/wp-content/uploads/2022/11/image-244.png)

Suppressing trailing newlines

## Printing Variable Values

When writing a script, you typically temporarily store a value to a variable, which you can print with the echo command. This function can be helpful when checking the value of a variable to debug a shell script.

Run the commands below to set the number variable’s value to 10 and print (echo) the value to the console.

```bash
# Stores the value of 10 to the number variable
number=10
# Prints the current value stored in the number variable
echo The number is $number
```

![Printing the Value of a Variable](https://adamtheautomator.com/wp-content/uploads/2022/11/image-245.png)

Printing the Value of a Variable

## Interpreting Escape Sequences

An escape sequence is a series of characters representing a special character, and you can use the -e option to interpret escape sequences.

For example, the \\t escape sequence represents a tab character. If you use the -e option and print \\t to the console, \\t will be interpreted as a tab character instead of being printed literally.

Some of the most common escape sequences are as follows:

<table><tbody><tr><td>Escape Sequence</td><td>Function</td></tr><tr><td>\b</td><td>backspace</td></tr><tr><td>\t</td><td>tab</td></tr><tr><td>\n</td><td>new line</td></tr><tr><td>\\</td><td>backslash</td></tr><tr><td>\$</td><td>dollar sign</td></tr></tbody></table>

Run the following command to print Hello\\tWorld to the console.

```bash
echo "Hello\tWorld"
```

You can see below that the echo command printed the string Hello\\tWorld as is.

![Printing a string without an escape sequence](https://adamtheautomator.com/wp-content/uploads/2022/11/image-246.png)

Printing a string without an escape sequence

Now, run the below command to print Hello\\tWorld, appending the -e option to interpret \\t as a tab character in between.

```bash
echo -e "Hello\tWorld"
```

Notice below the huge gap between the word Hello and World.

![Interpreting an escape sequence](https://adamtheautomator.com/wp-content/uploads/2022/11/image-247.png)

Interpreting an escape sequence

## Inserting Strings to a New Line

Printing a long single line of string makes a user struggle to read its entirety. Luckily, the \\n option lets you add a new line character in the middle of a string. This option is often used when printing multiple lines of text to the console, which gives clarity to the actual text.

Run the below command to print the string Echo command bash\\nAnother line, where the \\n option breaks the string into two lines.

```bash
echo -e "Echo command bash\nAnother line"
```

Below, you can see the \\n option put the string Another line to a new line.

![Inserting a string into a new line](https://adamtheautomator.com/wp-content/uploads/2022/11/image-248.png)

Inserting a string into a new line

## Inserting Vertical Tabs Between Strings

You have seen how the \\t escape sequence puts a horizontal tab character between strings. But how about a vertical tab? The \\v option lets you separate strings in vertical tabs, which is perfect for making text banners.

This option is often used when printing multiple columns of text to the console, with each column being on a new line with a new tab space at the beginning.

Run the below command to print the text Echo command bash demo, but with vertical tabs (\\v) between each word.

```bash
echo -e "Echo \vcommand \vbash \vdemo
```

![Inserting vertical tabs in between strings](https://adamtheautomator.com/wp-content/uploads/2022/11/image-249.png)

Inserting vertical tabs in between strings

## Printing File Names Based on Extension

If you are looking for a way to find files with a specific file [type](https://alexanders.com/blog/9-file-extensions-you-should-know-for-submitting-a-print-job/), this feature of the echo command works the same way. The echo command supports listing files in a directory with a particular extension with the wildcard character (\*).

Related:[The Many Ways On Ubuntu to Find Files](https://adamtheautomator.com/ubuntu-to-find-files/)

1\. Run the following command to print all files ending with the .txt extension in the current directory.

```bash
echo *.txt
```

![Printing all .txt files in the current directory](https://adamtheautomator.com/wp-content/uploads/2022/11/image-250.png)

Printing all _.txt_ files in the current directory

2\. Next, run the below command to print all files ending with the .deb file extension in the current directory.

Related:[Comprehensive Guide to Install Deb Packages on Ubuntu](https://adamtheautomator.com/install-deb-packages-on-ubuntu/)

```bash
echo *.deb
```

![Printing all .deb files in the current directory](https://adamtheautomator.com/wp-content/uploads/2022/11/image-251.png)

Printing all _.deb_ files in the current directory

3\. Lastly, run the below command to print the files with the .txt extension in the ~/Downloads directory. You can replace ~/Downloads/ with your file’s actual directory path.

```bash
echo ~/Downloads/*.txt
```

You can see below that the echo command supports specifying a directory path where your files are located.

![Finding .txt files in the specific directory](https://adamtheautomator.com/wp-content/uploads/2022/11/image-252.png)

Finding _.txt_ files in the specific directory

## Writing Output to File

When your command returns a massive output, you can barely read everything on the console. Writing the output to a file instead is a great idea. But how? Besides printing texts to the console, the echo command also supports writing outputs directly to a file.

Run the below command, which does not provide output since the output is written (>) to a file called _echo-bash.txt_ instead.

Note that if the _echo-bash.txt_ file already exists, it will be overwritten with the new content.

```bash
echo "echo command bash" > echo-bash.txt
```

Now, run the following cat command to view the contents of the _echo-bash.txt_ file.

```bash
cat echo-bash.txt
```

As you can see below, the echo command output is written to the _echo-bash.txt_ file.

![Verifying the output written to file](https://adamtheautomator.com/wp-content/uploads/2022/11/image-253.png)

Verifying the output written to file

## Appending Output to Existing Files

As previously mentioned, the > operator overwrites an existing file with new content. But what if you wish to append the output to an existing file instead of overwriting it?

Instead of a single > operator, make it two >>. This feature helps keep the existing content and append new entries to the end of the file.

Run the below command to append (>>) new texts (Appending text) to the existing echo-bash.txt file.

```bash
echo "Appending text" >> echo-bash.txt
```

Now, run the following cat command to view the contents of the _echo-bash.txt_ file.

```bash
cat echo-bash.txt
```

As you can see below, the echo command output is appended to the file without overwriting anything.

![Appending output to an existing file](https://adamtheautomator.com/wp-content/uploads/2022/11/image-254.png)

Appending output to an existing file

## Conclusion

The echo command in the bash shell is a simple but powerful tool that can be used in many ways. And in this tutorial, you have learned how to maximize the echo command’s potential with some of its most popular options.

Printing texts to the console is not just ‘printing texts’ anymore. You have seen how to write output to a file, append outputs, and change how each printed texts look.

Why not pick some ways you can use the echo command and write them on your [scripts](https://adamtheautomator.com/linux-shell-scripting-tutorial/)? See what creative ways you can come up with!

Related:[Your One and Only Linux Shell Scripting Tutorial](https://adamtheautomator.com/linux-shell-scripting-tutorial/)

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fecho-command-in-bash%2F&text=Echo%20Command%20in%20Bash%20Shell%20%3A%20Discover%20the%20Many%20Uses)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fecho-command-in-bash%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fecho-command-in-bash%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/06/Understanding-Bash-If-Else-and-other-Conditional-Statements.jpg)

### [Understanding Bash If Else and other Conditional Statements](/bash-if-else/)

Discover the ins-and-outs of Bash If Else and Bash Case scripting in this example-driven tutorial by ATA Learning!

![](https://adamtheautomator.com/wp-content/uploads/2022/11/bash-xargs.jpg)

### [Learning the Bash Xargs Command Through Examples](/bash-xargs/)

Bash Xargs is a complicated and useful command. In this ATA Learning tutorial learn to take advantage of its many uses!

![](https://adamtheautomator.com/wp-content/uploads/2022/07/How-to-Update-Ubuntu-IP-and-Hostname-via-Bash.jpg)

### [How to Update Ubuntu IP and Hostname via Bash](/change-linux-ip-hostname-bash-script/)

Discover and learn how to change a Linux IP and Hostname via a Bash script in this new tutorial by ATA Learning!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
