Table of Contents
In this article, we will go through 20 Practical echo command examples in Linux for Beginners. echo utility is a part of GNU Project and is by default available on all Kind of Linux Systems. This is a very handy tool which can be used from command line as well as in bash or shell script. It takes an input and display it as a line of text. We will understand more about this tool using some real world examples in below section.
Synopsis
echo [SHORT-OPTION]... [STRING]...
echo LONG-OPTION
Practical echo command examples in Linux
Also Read: 15 Popular diff command examples in Linux(Compare two files)
Example 1: How to check echo command version
To check the version of echo command you need to use /bin/echo --version
command as shown below. As you can see from the output, current echo version is 8.22
.
[root@localhost ~]# /bin/echo --version echo (GNU coreutils) 8.22 Copyright (C) 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Brian Fox and Chet Ramey.
Example 2: How to Display a Statement using echo command
To display a statement on the output, you need to put that statement under double inverted comma("")
and use it with echo
command as shown below.
[root@localhost ~]# echo "Hi, How are you?" Hi, How are you?
Example 3: How to Add tab spaces between words in a statement
If you want to add horizontal tab spaces between words in a statement then you need to use \t
sequence with echo statement as shown below.
[root@localhost ~]# echo -e "Hi, \tHow \tare \tyou?" Hi, How are you?
-e : enable interpretation of backslash escapes. More on echo Man Page.
Example 4: How to display statement through a variable
Instead of displaying a statement directly, you can also choose to store the statement in a variable and then display the contents of the variable using echo
command as shown below. In this example, we are storing "Hi, How are you ?"
statement in a temporary variable x
and then displaying it using echo $x
command.
[root@localhost ~]# x="Hi, How are you ?" [root@localhost ~]# echo $x Hi, How are you ?
Example 5: How to Add a New Line Before the Statement
You can add a new line(\n)
before starting the statement with echo command as shown below. Here we have added \n
sequence before the statement "Hi, How are you ?"
which will put a newline before displaying this statement on the output.
[root@localhost ~]# echo -e "\nHi, How are you?" Hi, How are you?
Example 6: How to Add a New Line After the Statement
You can add a new line(\n)
after starting the statement with echo
command as shown below. Here we have added \n
sequence after the statement "Hi, How are you ?"
which will put a newline after displaying this statement on the output.
[root@localhost ~]# echo -e "Hi, How are you?\n" Hi, How are you?
Example 7: How to Add a New Line Before and After the Statement
Like above example, you can add a new line before and after the statement. Wherever you want to add a new line, you need to provide \n
sequence as shown below. Since here we want to add a new line before and after the statement, so one line has been given before Hi
word and another new line after you?
word.
[root@localhost ~]# echo -e "\nHi, How are you?\n" Hi, How are you?
Example 8: How to Add Vertical tabs in a Statement
If you want you can also add vertical tabs in your statement using \v
sequence. Here we are adding vertical tabs before every word in the statement "Hi, How are you?"
. This will add a tab vertically in the statement as shown below.
[root@localhost ~]# echo -e "\vHi, \vHow \vare \vyou?" Hi, How are you?
Example 9: How to Add Backspace in a Statement using echo command
You can also add a single backspace(\b)
or multiple backspace(\b\b\b)
in your statement with echo command as shown below. In this example, we are adding a single backspace before every word in the statement.
[root@localhost ~]# echo -e "\bHi, \bHow \bare \byou?" i,Howareyou?
Example 10: How to Add form feed in a Statement
To add form feed between the words in a statement, you can add \f
sequence. In this example, we are adding form feed \f
sequence before every word to force it to start from new page as shown below.
[root@localhost ~]# echo -e "\fHi, \fHow \fare \fyou?" Hi, How are you?
Example 11: How to use echo command in a Script
Like you can use echo command in a terminal, similarly you can use echo command in a script. Here we are creating a bash script file example.sh
and adding a single line echo statement "Hi, How are you ?"
. Then we are making the script executable by providing +x permission using chmod +x example.sh command. Now if you run the script using ./example.sh
, you will see echo statement on the output.
[root@localhost ~]# vi example.sh #!/bin/bash echo "Hi, How are you?" [root@localhost ~]# chmod +x example.sh [root@localhost ~]# ./example.sh Hi, How are you?
Example 12: How to List all the Files and Folders in Current directory
If you want to list all the files and folders in current directory, then you need to use echo *
command as shown below.
[root@localhost ~]# echo * anaconda-ks.cfg example example.sh file1.txt file2.txt file.txt sample-project-repo test
Example 13: How to List all the Files of a Specified Directory
If you want to list all the files of a specified directory then you can just give the path of that directory with echo
command and list out all the files. In this example we are listing all the files under example directory using echo example/*
command as shown below.
[root@localhost ~]# echo example/* example/hello.txt example/test
Example 14: How to check all the options available with echo command
If you want to check all the options available with echo command then you need to use /bin/echo --help
command as shown below.
[root@localhost ~]# /bin/echo --help Usage: /bin/echo [SHORT-OPTION]... [STRING]... or: /bin/echo LONG-OPTION Echo the STRING(s) to standard output. -n do not output the trailing newline -e enable interpretation of backslash escapes -E disable interpretation of backslash escapes (default) --help display this help and exit --version output version information and exit If -e is in effect, the following sequences are recognized: \\ 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)
Example 15: How to avoid the trailing new Line
If you want to ignore the new line used in the statement, then you need to use -n
option with echo
command to ignore all the trailing new lines as shown in below example.
[root@localhost ~]# echo -n "Hi, \nhow \nare \nyou?" Hi, \nhow \nare \nyou?
-n : do not output the trailing newline.
Example 16: How to Display the value of Bash Shell Inbuilt Variables
Sometimes you might want to see the value of Bash Shell Inbuilt variables. For example, if you want to see the value of $SHELL
variable then you need to run echo $SHELL
command as shown below.
[root@localhost ~]# echo $SHELL /bin/bash
Example 17: How to redirect echo command output to a File
There might be a scenario when you want all the echo statement output to be redirected to a file instead of showing them on the output, so that you can revisit that file later whenever you need it. This task can be easily performed by using redirection operator. In this example, we are redirecting echo command output "Hi, How are you ?"
to file.txt
using echo "Hi, How are you?" > file.txt
command. Here redirection operator(>)
redirected all the output to a file named as file.txt
.
[root@localhost ~]# echo "Hi, How are you?" > file.txt [root@localhost ~]# cat file.txt Hi, How are you?
Example 18: How to Use Carriage Return with echo command
If you want to start a line from certain place then you can make use of carriage return with your echo command. In this example, we want our line to start from "How are you ?"
so we have put carriage return just after the "Hi"
word as shown below.
[root@localhost ~]# echo -e "Hi, \r How are you ?" How are you ?
Example 19: How to Use ANSI Escape Sequence to change color of the output
You can also use ANSI Escape sequence with echo command to change the color of the output. In this example we are changing the color of output GREEN
using below given escape sequence.
[root@localhost ~]# echo -e '33[0;32mGREEN' GREEN
Example 20: How to Check Man Page of echo command
If you want to check the man page of echo command then you need to use man echo
command as shown below.
[root@localhost ~]# man echo ECHO(1) User Commands ECHO(1) NAME echo - display a line of text SYNOPSIS echo [SHORT-OPTION]... [STRING]... echo LONG-OPTION DESCRIPTION Echo the STRING(s) to standard output. -n do not output the trailing newline -e enable interpretation of backslash escapes -E disable interpretation of backslash escapes (default) --help display this help and exit