For your better understanding first, let me give you a brief idea about Echo Command. Now, this is actually a fundamental command seen in most operating systems that offer a command line.
These commands are often used in scripts, batch files, and as a section of particular commands, any place you need to insert a text.
There are numerous command shells including bash, ksh, and csh that implement echo in the form of a default command.
Echo is basically a Unix and Linux tool for commands that is used for presenting lines of texts or string that are usually passed as arguments on the command line.
This article will let you know the various ways of using the Echo command.
At first, the basic syntax for writing the Echo command should be known. This is given below.
echo [SHORT-OPTION]… [STRING]…
echo LONG-OPTION
How to use Echo Command Linux?
Here are the various techniques with which you will be able to use the Echo Command line. Here they are listed.
Exposing a String on the Terminal to View
In order to print a text or a string on the terminal, you need to make the use of the syntax: echo [string]
Here is the command line.
$ echo “Welcome to Linux”
Making a Declaration on a Variable and Echo the Value
Let us suppose here that you have a variable x that is designated the value 45, that is,
$ x=10
You will now be able to print out the value of the variable called x by running the command line mentioned below.
$ echo The value of x is $x
The output can be somewhat like this:
The value of x is 10
Eliminating Gaps or Spaces in between text by the Use of -e and \b Choices
You can go for opting to remove spaces by using the option called \b along with the -e parameter.
Here is something important that you need to take a note of. The parameter -e is actually applied for the interpretation of backslash interpreters.
Here is a text string as a sample:
Linux is an opensource operating system
If you want to remove the spaces between each and every word, you need to write the following command as shown below.
$ echo -e “Linux \bis \ban \bopensource \boperating \bsystem”
So, the outcome after executing this command line will be this:
Linuxisanopensourceoperatingsystem
Building up Fresh Lines in between Text using \n Option
For bringing a new line into being after each and every word in a string, you have to use the -e operator along with \n option as displayed here below.
$ echo -e “Linux \nis \nan \nopensource \noperating \nsystem”
The output of this will be like as shown below.
Linux
is
an
opensource
operating
system
Applying Horizontal Tab Spaces in Between Text Using \t Option
For inserting tab spaces in between words in a text string you need to use the -e operator along with the \t option as represented below.
$ echo -e “Linux \tis \tan \topensource \toperating \tsystem”
The output after the command line is executed will be this:
Linux is an opensource operating system
Insertion of Vertical Tab Spaces with the Use of the \v Option
You can do this to show your special and creative character inside you. Be a bit creative and make the output more attractive. For this, here, you have to create vertical tab spaces with the application of the -e operator. Along with that, you have to use the \v option. Here it is shown below.
$ echo -e “Linux \vis \van \vopensource \voperating \vsystem”
And here is the output of the above command line.
Linux
is
an
opensource
operating
System
Inserting Double Vertical Tab Using \n and \v Option at the Same Time
With the use of \n and \v options at the simultaneously will help you to add double vertical tab gapping or spacings. Here is the command line that will let you do the needful.
$ echo -e “Linux \n\vis \n\van \n\vopensource \n\voperating \n\vsystem”
And this is the output you will be getting after the execution of the command line:
Linux
is
an
opensource
operating
system
Printing with the Use of * Option
All the files and folders can be printed in your present working directory. This becomes plausible with the use of the following command.
$ echo *
In order to print out files of a particular kind, you have to run the following command line.
$ echo *.file_extension
Here is an example: $ echo *.pdf
The Use of the Carriage Return ‘\r’ Option
The ‘r’ option will provide you the carriage return which is any wor before the \r are removed in the output. Here is the command line that you have to enter.
$ echo -e “Linux \r is an opensource operating system”
And this will be the output for this command line.
is an opensource operating system
Eliminating Echoing Trailing Newline
For this, you need to use the -n option for removing trailing newline. Here is an example to clear things up.
$ echo -n “Linux is an opensource operating system”
According to this particular command line, you will be getting this output shown below.
Linux is an opensource operating [email protected]:/$
Thus, this guide for Linux command Echo I think is enough for you to have a clear understanding of this particular fundamental command.
Ending Note
I hope the examples that I showed you were helpful enough for you to get a grip on the Echo command.
Comment in the section given below to let us know what do you think about this piece. Feedback from you guys will always be a great support and help.