Shell Programming - Echo

Unix

Share

In this tutorial we will learn about echo command in Shell Programming.

Open your terminal and lets write some shell script.

Example #1: Echo "Hello World" text

In the following example we will echo "Hello World" text.

#!/bin/sh
echo "Hello World"

Output:

$ sh example01.sh 
Hello World

So, in the above code we are passing a string argument to the echo command and it is getting echoed in the terminal.

Example #2: Echo Hello World

In the following example we will echo Hello World.

#!/bin/sh
echo Hello World

Output:

$ sh example02.sh 
Hello World

In the above code we are passing two arguments Hello and World to the echo command. So, when the script is executed it echoes the text Hello World.

Note! The echo command automatically puts a single space between its parameters (arguments).

Example #3: Echo "Hello    World"

In the following example we will echo "Hello    World" with extra spaces in between the words.

#!/bin/sh
echo "Hello    World"

Output:

$ sh example03.sh 
Hello    World

The string "Hello    World" with 4 spaces between the two words is treated as a single parameter (argument) and so, the echo command prints it as is.

Example #4: Echo Hello    World

In the following example we are giving extra spaces and tabs between the two words "Hello" and "World".

#!/bin/sh
echo Hello    World

Output:

$ sh example04.sh 
Hello World

Even though we have given extra spaces and tabs between the word Hello and World, the echo command treats them as two parameters and so, when it is echoed in the terminal we only get one space between the two words.

Example #5: Echo Hello *

#!/bin/sh
echo Hello *

Output:

$ sh example05.sh 
Hello example01.sh example02.sh example03.sh example04.sh example05.sh

In the above code we are using * which has a special meaning and when we pass that to the echo command it prints all the files in the current directory.

So, in the above example we are passing two arguments Hello and * to the echo command and so we get "Hello" and the file names as output.

Example #6: Echo Hello \*

#!/bin/sh
echo Hello \*

Output:

$ sh example06.sh 
Hello *

In the above code we are escaping the character * using backslash. So, the * is no longer treated as a special character to list all the files in the current directory.

And so, the echo command gets two arguments Hello and \* which then gets echoed as Hello *.

We can achieve the same output if we put the * in quotes like the following.

#!/bin/sh
echo Hello "*"

Example #7: Echo *sh

#!/bin/sh
echo *sh

Output:

$ sh example07.sh 
example01.sh example02.sh example03.sh example04.sh example05.sh example06.sh example07.sh

In the above code we are passing *sh argument to the echo command so, echo will list all the files that ends with sh. This is because * is a special character.

To echo *sh as the result we have to either escape * by using \* or use quotes like "*sh"

The following example we will print *sh.

#!/bin/sh
echo \*sh
echo "*sh"
echo '*sh'

Example #8: Echo "Value of n = $n"

In this example we will take value of n from user and then print that value using echo.

#!/bin/sh
echo "Enter some integer value: "
read n
echo "Value of n = $n"

Output:

$ sh example08.sh
Enter some integer value: 
10
Value of n = 10

To get the value stored in the variable n we use the $ sign. So, the echo command is getting a string argument "Value of n = $n" and $n is getting replaced by the value stored in the the variable n.

We can achieve the same result by write the following code and putting $n out of the quotes.

#!/bin/sh
echo "Enter some integer value: "
read n
echo "Value of n = " $n

In the above case we are passing two arguments to the echo command, a string "Value of n = " and value of variable n i.e., $n.

Share