Printing Something – echo, clear

  • echo: To print something
  • clear: To clear the current screen in the terminal

Type these commands excluding the dollar sign ($) in your terminal one by one, and observe the output by hitting enter:

$ echo Hello World! # dollar sign '$' and space included to represent command line
$ echo Hello World!! # Hash or Pound is used for commenting.
$ echo Hello World!!!

The first command simply prints Hello World! Then for each increase in ‘!’, the output starts behaving weirdly. This is because the exclamation mark is one of the many special characters in Bash language, which has some special meaning. To get our work done, we need to wrap the text inside single quotes (‘ ‘).

$ echo 'Hello World!'
$ echo 'Hello World!!'
$ echo 'Hello World!!!'

Here’s the sample code. The appearance may differ for you, but the concept remains the same.


Finally, if you want to clear everything printed in the screen, run the command clear, and you’ll get a fresh, clean screen. Remember, you can find previous commands and outputs by just scrolling upward.

In-Depth Explanation

You’ve already known what you need to understand to move forward. But if you’re interested as me, let’s try to understand some detail and other related things.

Why more than one explanation mark behaved weirdly?

There’s a term in bash called History Expansion. ‘!’ is used for this task. It means, adding your previous command to the end of the present one. For example, when you typed:

$ echo Hello World!

It printed Hello World! as expected. Then when you typed:

$ echo Hello World!!

bash expanded the history, i.e. added the previous command at the end of the present command. So it became:

$ echo Hello World!! # Actual command
echo Hello Worldecho Hello World! # Expanded command
                                  # Notice the first part is actually the present command, 
                                  # and the last part is actually the previous command
Hello Worldecho Hello World!      # Executed command

But why there were 2 outputs in the terminal? Notice, the first one is for History Expansion, and the second one is the executed form of the expanded form. So we got Hello Worldecho Hello World! as final output.

  • You can turn of History Expansion in bash if you want.

Can I Use Double Quotes When Printing in Bash?

At first it’ll look good, then weird, and then powerful when you understand it properly. So pay attention. Try out these commands:

$ echo "Hello World!"
$ echo "Hello World!!"

The output should look like this:


The first output looks good, it’s same as using single quotes. But the second output looks weird, it’s same as not using any quotes. So do we really use double quotes in bash scripting? And how is it powerful as stated above? You’ll see this in the next section.

Difference between single quotes (‘ ‘) and double quotes (” “) in Bash Scripting

Make sure you understand these concepts:

  • String: In programming, string is a sequence of characters. For example, “Hello”, “World”, “Hello World!”. The characters can include variables that contain some values.
  • Raw String: A string without any special meaning. It means what it looks like.
  • Variable: A string that represents value for others

Single Quotes

Enclosing characters or string withing single quotes makes the characters a raw string. So it means what is looks like, does not have any special meaning. For example, the command $ echo ‘Hello World!!!’ printed Hello World!!!, and it didn’t contain any special meaning of those exclamation marks.

Sometimes in programming, you use backslash to escape the execution of special characters, which is known as escape characters. But single quotes in Bash scripting is so straightforward that it even doesn’t recognize any escape character.

If you have any variable inside your string, single quotes can’t also recognize them. It treats the variables as simple characters or string. Try out the commands and observe the outputs.


In the second command, the single quote of I’m worked as the closing quote of the single quotes. That’s why when you hit enter, a new line was waiting for a command. This new line starting with ‘> represents that your command is not completed yet. So you must type something that completes the command, otherwise this ‘>’ sign will keep popping up. I had to type the ending single quote and hit enter.

Tip: If you’re stuck and don’t know how to complete the command, you can try ctrl+Z or command+Z to force end the command

In the third command, I used escape character backslash quote (\’) but it worked as raw string and completed the single quotes. So I again fall into same problem.

In the fourth command, I actually used a variable situation to store a value fine. But in fifth command, as the string working as raw string, it didn’t print I am fine, rather it just printed the text.

Double Quotes

Enclosing a string in double quotes (” “) preserves the characters, executes the value of variables, as well as contains the special meaning of the characters $ (dollar sign), ` (tilde or backtick), \ (backslash), ! (exclamation mark, when history expansion is enabled) etc. So it’s all in one! See the examples for a better understanding. Read this if you want to know more.

up 5

Tips and Cautions

  • When assigning value to a variable, don’t use any space around the equal sign. situation=tired will work when situation = tired won’t. This is because Bash separates multiple commands by spaces.
  • If you’re using a string as a variable that includes white space, then wrap the string with single or double quotes. You know which one to use. For example, situation=so tired will throw an error while situation=’so tired’ will work fine.
  • You can use upward and downward keys to move around previous commands. It will save you some time if you need to use a similar command more than once.
  • To terminate a command, use ctrl+Z or command+Z
  • To exit a window, type exit in the command line

Mind Sharing It?
Join Facebook Group

Public Group

A group optimized for learning purposes. Track your progress by joining a unit

Follow on Quora

Public Space

Best place to ask questions! Join the built-in community to get help from others