Let’s see what is Echo command in Linux.
Echo command mainly prints on the screen whatever you ask it. It is a simple function but most of the scripts would be incomplete with echo command. You won’t be able to get visible output from the shell script, without the echo command.
Syntax of the echo command
echo [option] [string]
Let’s see the echo command in Linux with some examples which will help us to understand it better.
1. Check the version
You can check the version of the echo command using –version options
$ /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.
2. Printing using the echo command
How to print using the echo command? it’s actually quite simple. Just use the echo command following the text as shown in the example below.
$ echo "Hello JustGeek" Hello JustGeek
3. Printing Variables.
Along with the text, we can also print the content of variables. For example, if you have some value stored in a variable, you can directly print that value.
In the example below we have a variable named
IamVariable which is storing string
This is JustGeek. So let’s see what happens if we do echo on
$ IamVariable="This is JustGeek" $ echo $IamVariable This is JustGeek
You can also run a command through the echo command and print its output to the screen. For example, if you can directly run a command and store it in variable
In the example below we are storing the date command output
IamVariable and if we do run echo on that we will see the output.
$ IamVariable=$(date) ; echo $IamVariable Tue Jun 21 12:27:51 EDT 2022
4. Formatting using Echo Command.
You can use formatting functions with echo using the option
-e. It enables the interpretation of backslash escapes.
1. Printing on a new line
"\n" this parameter will print on the new line. As shown in the below example
[[email protected] ~]# echo -e "First Line\nSecond Line" First Line Second Line [[email protected] ~]#
2. Adding tabs
You can easily add tabs between two words, for instance. Look at the example below it added tabs between Hello and World.
$ echo -e 'Hello, \tWorld!' Hello, World!
3. Vertical Tabs
You should also be able to use vertical tabs.
$ echo -e 'Hello, \vWorld, \vthis \vis \vJustgeek!' Hello, World, this is Justgeek! [[email protected] ~]$
Above are just a few examples, but in fact, there are multiple characters that can be used that suit you. See the full list below.
\a: Alert (historically known as BEL). This generates the default alert sound. \b: Writes a backspace character. \c: Abandons any further output. \e: Writes an escape character. \f: Writes a form feed character. \n: Writes a new line. \r: Writes a carriage return. \t: Writes a horizontal tab. \v: Writes a vertical tab. \\: Writes a backslash character.
As you all know echo command is utility when it comes to shell scripting. You should also see the find command if you haven’t seen it already. Just for fun, you should also see what the actual Echo means.
1 thought on “Echo Command in Linux”