Skip to content
Related Articles

Related Articles

Shell Script to Show the Difference Between echo “$SHELL” and echo ‘$SHELL’
  • Last Updated : 28 Mar, 2021

When shell commands are executed with the help of the file this is called shell scripting. In this article, we will understand how shell commands work? How to use commands more efficiently? And understand the logic of interpretation of commands on shell. Bash or Bourne Again Shell is the most widely used shell, it comes pre-installed in most of the Linux distributions.

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Single quote Interpretation:

The single quote will not interpret anything like variables, backslash, etc. into other forms. For example:

# h = 5
# echo '$h'

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

$: $ sign is used in the shell to retrieve the value of variables.



echo: echo command is used to print the text or string to the shell or output file.

Here we initialized a variable name “h” and 5 to it, and then we used the echo command to print the value using $ sign in single quotes. Now let us try this on a predefined variable named SHELL, this predefined variable stores the path of the shell.

# echo '$SHELL'

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Even though the variable SHELL contains the path of the shell it did not print the path on output because we used single quote ‘ ‘.

Shell Script:

Same thing we could do using a file and making it executable and running it will produce similar output.

We will use:

  • nano: It is a command-line text editor for Linux distros.
  • cat: cat command will be used to see the content of a file.
  • chmod: This command will be used to grant permission to be executable.

Open a file in nano editor name it program.sh. “.sh” extension is used for shell script files.

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’



Write content to file:

h = 5
echo '$h'

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Press ctrl + o to save and press ctrl + x to exit, now give permission to program.sh file to execute:

# chmod 777 program.sh

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Run the file:

# ./program.sh

Double quote Interpretation:

The double-quote will interpret commands with variables, backslash etc. Double quotes are really helpful when working with various variables and user inputs. Let’s take the same examples:

# h = 5
# echo "$h"

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Yes, this time the real value of h is printed as ” ” interpret the special cases present in the statement. Similarly, now just check it with the predefined variables like SHELL.

# echo "$SHELL"

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Shell Script:

Now let us try the whole thing using a shell script file.

Open a file name newProgram.sh using nano editor:

# nano newProgram.sh

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Write the content given below:

h = 5
echo "$h"

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Give permission to make it executable:

# chmod +x newProgram.sh

Run the file to see the output:

# ./newProgram.sh

Shell Script to explain the difference between echo “$SHELL” and echo ‘$SHELL’

Below is the table to understand the behavior of single and double quotes. First, we initialize some variables.

# name = (hemant)
# number = 40
InputOutputInterpretation
echo “$namehemantVariable get interpreted
echo ‘$name$nameLiteral meaning get printed
echo “$number40interpreted
echo ‘$number$numberNo interpretation of variable
echo ‘“$number“$number”” ” is treated literally inside ‘ ‘
echohello\t$namehello\themantInterpreted but by default escape sequence in off
echo -ehello\t$namehello    hemantWorked with -e option
\’\’\’ is interpreted inside ” “ but has no significance for
My Personal Notes arrow_drop_up
Recommended Articles
Page :