16 Echo Command Examples in Linux

In this guide, we will explain Linux echo command with 16 practical examples.

Echo command in Linux is one of the widely used command in day-to-day operations task. The echo command is a built-in command-line tool that prints the text or string to the standard output or redirect output to a file. The command is usually used in a bash shell or other shells to print the output from a command. Echo command is also used frequently in bash shell scripts.

Echo command Syntax

$ echo [option] [string]


1) Display a simple message on the terminal

To print a simple text message on the terminal with echo command , run the command:

$ echo Hello Guys


2) Print out the value of a variable with echo command

Let’s assume that you have initialized a variable x and assigned it a value of 100 as shown

$ x = 100

You can echo its value by prefixing the variable name with a dollar sign as shown

$ echo The value of x is $x


3) Print a line of text comprising of double quotes

To print a line of text with double quotes with echo command, simply enclose the phrase in double-quotes between single quotes.

$ echo Hello guys welcome to ' "Linuxtechi" '


4) Display a line of text consisting of a single quote

If you wish to print a line with a word containing a single quote, enclose the word inside double quotation marks as shown:

$ echo Hey, "We're" Linuxtechi, a community driven site.


5) Redirect echo command output to a file

To redirect echo command output to a file instead of printing it on the screen use the greater than( > ) and double greater than ( >> ) operators.

When you use the greater than( > ) operator, the file will be overwritten. If the file does not exist, it will be created.

$ echo Hey guys, Welcome to Linuxtechi > greetings.txt


The double greater than operator ( >> ) appends text to a file. For example, to add an entry to the /etc/hosts files use the syntax shown

$ echo  web-server-01 >> /etc/hosts

6) Use echo command to match identical files

You can use echo command with a wildcard character to return identical files i.e files that bear the same file extension. For example, to print out all text  files with the .txt extension, run the command.

$ echo *.txt


7) List all files and folders in the current directory

The echo command can act as the ls command and list all the files and folders in your current directory using the wildcard as shown:

$ echo *


8) Create new lines using the echo command

Using the backslash interpreter -e, you can manipulate how the line appears on the output. For example, to print a new line, use the ‘\n‘ escape character option as shown below:

$ echo -e  "Welcome \nto \nLinuxtechi  \ncommunity"


Also Read8 Head Command Examples in Linux

9) Create tab spaces between words in a sentence

Apart from creating a new line, you can increase the spacing between words in a sentence using the ‘\t‘ or TAB option as shown.

$ echo -e  "Welcome \tto \tLinuxtechi  \tcommunity"


10) Combine new line and tab spacing options in echo command

You can combine the ‘\n’ and ‘\t’ options at the same time as shown:

$ echo -e  "\n\tWelcome \n\tto \n\tLinuxtechi  \n\tcommunity"


11) Create vertical tab spaces in echo command output

The \v option enables you to create vertical tab spaces as shown:

$ echo -e  "Welcome \vto \vLinuxtechi  \vtcommunity"


12) Combine new line and vertical tab spaces in echo command

Additionally, you can experiment and combine the ‘\n’ and ‘\v‘ options as shown.

$ echo -e  "Welcome \n\vto \n\vLinuxtechi  \n\vtcommunity"


13) Use the carriage return option

The \r option omits the preceding text. For example, the output of the command below omits the first 2 words.

$ echo -e  "Welcome to \rLinuxtechi  community"


14) Truncate echo command output of text

To suppress any further output and proceed without going to the next line use the ‘\c’ option as shown:

$ echo -e  Welcome to Linuxtechi  \ccommunity


15) Remove all spaces from the text string with echo command

Use ‘\b’ option along with -e option in echo command to remove all the spaces from the text string, example is shown below:

$ echo -e "Welcome \bto \bLinux \bCommunity"

16) echo command usage in bash shell script

As we have already stated that echo command is frequently used in bash shell scripts. Example of echo command usage in shell script are listed below:

$ cat echo-bash.sh
# Echo Command Usage in Script
os_version=$(grep -i "PRETTY_NAME" /etc/os-release  | awk -F'=' '{print $2}' | sed 's/"//g')
no_cpus=$(lscpu  | grep  '^CPU(s):' | awk -F":" '{print $2}' | sed "s/^[ \t]*//")
total_mem=$(grep MemTotal /proc/meminfo | awk -F":" '{print $2}' | sed "s/^[ \t]*//")
echo 'OS Version :' $os_version
echo 'Number of CPUs :' $no_cpus
echo 'Total Memory :' $total_mem

Output of above script when it is executed:

$ bash echo-bash.sh
OS Version : CentOS Linux 7 (Core)
Number of CPUs : 4
Total Memory : 8008968 kB

And that’s it. Thank you for taking your time in this guide. Hopefully, you are now better informed.

Also Read : 14 grep command examples in Linux

Share on:

James Kiarie, a certified Linux administrator and a passionate tech enthusiast who stays connected with the latest trends in the tech world. Beyond executing commands on the terminal, you'll find me enjoying some cool music, taking casual strolls, or indulging in a good movie.

1 thought on “16 Echo Command Examples in Linux”

Leave a Comment