Saturday, 28 September 2019

Echo Command in Linux with Examples

Echo Command, Linux Certifications, Linux Tutorials and Materials, Linux Guides, LPI Online Exam

Echo is a Unix/Linux command tool used for displaying lines of text or string which are passed as arguments on the command line. This is one of the basic command in linux and most commonly used in shell scripts. In this tutorial, we will look at the different  options of echo command.

Basic Syntax


echo [option] [string]

1) Displaying a string on the terminal

To print text or a string on the terminal, use the syntax

echo [string]

$ echo "Welcome to Linux"

Sample Output

Welcome to Linux

2) Declare a variable and echo it value

Assume you have a variable x which is assigned the value 45  i.e.

$ x=10

You can print the value of variable x by executing the command

$ echo The value of x is $x

Sample Output

The value of x is 10

3) Remove  Spaces in between text using -e and \b options

You can choose to remove spaces using the \b option in conjunction with the -e parameter.

Note:

The -e parameter is used for the interpretation of backslash interpreters

Let's assume you have the text string

Linux is an opensource operating system

To remove spaces between the words, run

$ echo -e "Linux \bis \ban \bopensource \boperating \bsystem"

Sample Output

Linuxisanopensourceoperatingsystem

4) Create new lines in between text  using \n option
To create a new line after each word in a string use the -e operator  with the \n option  as shown

$ echo -e "Linux \nis \nan \nopensource \noperating \nsystem"

Sample Output

Linux
is
an
opensource
operating
system

5) Create horizontal tab space in between text  using \t option

If you want to create tab spaces in between words in  a string use the  -e operator  with the \t option  as shown

$ echo -e "Linux \tis \tan \topensource \toperating \tsystem"

Sample Output

Linux is an opensource operating system

5) Create vertical tab spaces using the \v option

You can decide to get a bit fancy and create vertical tab spaces using the -e operator  with the \v option  as shown

$ echo -e "Linux \vis \van \vopensource \voperating \vsystem"

Sample Output

Linux
       is
           an
               opensource
                          operating
                                     system

6) Double vertical tab using \n and \v option simultaneously

You can double the vertical tab spacing as shown below using the \n and \v options as shown

$ echo -e "Linux \n\vis \n\van \n\vopensource \n\voperating \n\vsystem"

Sample Output

Linux

is

an

opensource

operating

system

7) Print all files and folders using the * option

You can print all files and folders in your current working directory using the command

$ echo *

This has the same output as the ls command

Sample Output

Echo Command, Linux Certifications, Linux Tutorials and Materials, Linux Guides, LPI Online Exam

To print files of  a specific type run

$ echo *.file_extension

For example

$ echo *.pdf

Sample Output

Echo Command, Linux Certifications, Linux Tutorials and Materials, Linux Guides, LPI Online Exam

8) Using the carriage return '\r' option

The 'r' option gives you the carriage return i.e. any word(s) before the \r are omitted in the output

$ echo -e "Linux \r is an opensource operating system"

Sample Output

is an opensource operating system

9) Omit echoing trailing newline

The -n option is used for omitting trailing newline. This is shown in the example below

$ echo -n "Linux is an opensource operating system"

Sample Output

Linux is an opensource operating systemjames@buster:/$

Related Posts

0 comments:

Post a Comment