Shell Scripting Tutorial – Quotes and Comments

Comments in Shell Scripting

The line begins with # (hash) is nothing but commenting in shell scripts which is used to ignore the whole line ahead. These lines are the statements for the scripts which do not be executed. These notes are called comments. It is nothing but explanatory text about script. It makes scripts easier to understand.

#!/bin/bash

# This is a comment! 

echo Hello World # This is a comment, too!

Single Quotes

Single quotes (don’t confuse with the back quotes) serve to remove (escape) the special meaning of all characters enclosed by them. Thus, the following statement would work as follows:

$ echo 'My current directory is: `pwd`' [Enter]

My current directory is: `pwd`

Double Quotes

Double quotes (again don’t confuse with the back quotes or single quotes) serve to remove (escape) the special meaning of all characters enclosed by them, except for the $ (dollar sign) , the \ (backslash character) and the ` (back-quote  symbol). Thus, the following statement would work as follows:

$ CUR_DIR=`pwd` [Enter]

$ echo "My current directory is: $CUR_DIR" [Enter]

My current directory is: /home/mthomas

Note:  With single quotes in this example, the value stored in the $CUR_DIR variable would not be displayed. The best practice is to always enclose text and variables within double quotes and escape any special characters using the backslash character.

Note that if any pair of quotes is unmatched then a situation may arise where a command results in single greater than (>) character displays as follows:

$ echo "My current directory is: $CUR_DIR [Enter]

The > character in this example is the shell environment variable named PS2. Here don’t confuse with an output redirection character. When this occurs, the shell is trying to parse the command entered and is missing one or more characters it needs to complete its parsing.

Back Quotes

The back quotation mark character (i.e. `) when used in pairs enclosing a command serve to perform command substitution. That is, when used as follows `command`. The command’s output is substituted at the location of the leftmost backquote. Here note that these are the different character than the single quote. For example, if we wanted to output:

My current directory is : current directory location

we could try the following:

echo My current directory is : pwd [Enter]

But this would result in:

My current directory is : pwd

To get what we want, we could use back quotes as follows,

$ echo My current directory is : `pwd`

My current directory is : /home/ravi

Note: The / in /home/ravi is substituted exactly at the location of the leftmost backquote.

Backslash

The backslash character (\) has two use cases (differs from the front-slash (/) character). The first use is for line continuation. If a line of shell commands becomes very long then it is useful to continue it across more than one line. Here note that if you want to do this then the split cannot be in the middle of a word, it must be done in appropriate whitespace.

The Second use of backslash character is to remove the special meaning of the following single character. Sometimes it is considered as escaping the special meaning of the following character. Characters having special meaning are referred to as metacharacters. This is because the * is a metacharacter and without using the backslash to remove its special meaning will results an error.

$ echo The $? variable returns the child exit status [Enter]

The 0 variable returns the child exit status where what we really want is:

$ echo The \$? variable returns the child exit status [Enter]

The $? variable returns the child exit status

Read Also : Shell Scripting Tutorial – Basic Concepts of Shell Scripts

Share on:

Ravindra Kumar

Hi, This is Ravindra. I am founder of TheCodeCloud. I am AWS Certified Solutions Architect Associate & Certified in Oracle Cloud as well. I am DevOps and Data Science Enthusiast.

Leave a Comment