Open In App

Shell Script to Show the Difference Between echo “$SHELL” and echo ‘$SHELL’

Last Updated : 24 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In shell scripting and Linux, the echo command is used to display text on the terminal or console. When used with the $SHELL variable, which contains the path of the current user’s shell program, the output of the echo command can be different depending on whether the variable is enclosed in single or double quotes.

echo “$SHELL” will expand the $SHELL variable and display the value of the variable on the terminal. The double quotes allow for variable expansion, so the output will be the path of the current user’s shell program.

echo ‘$SHELL’ will not expand the $SHELL variable and will display the literal text $SHELL on the terminal. The single quotes treat all characters within the quotes as literal text, so the output will be the string “$SHELL” itself, rather than the value of the variable.

It is important to note that using single or double quotes can affect the way variables and special characters are interpreted in shell scripting and Linux. As mentioned in the previous answer, single quotes treat all characters within the quotes as literal text, while double quotes allow for the expansion of variables and special characters.

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.

Difference Between echo “$SHELL” and echo ‘$SHELL’ are as follows:

Feature  echo “$SHELL”  echo “$SHELL” 
Variable expansion  Yes  No
 
Special character expansion  Yes  No
 
Output  Displays the value of the $SHELL variable  Displays the string “$SHELL”
 
Use case  When the value of the $SHELL variable needs to be displayed  When the literal string “$SHELL” needs to be displayed
 

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
Input Output Interpretation
echo “$name hemant Variable get interpreted
echo ‘$name $name Literal meaning get printed
echo “$number 40 interpreted
echo ‘$number $number No interpretation of variable
echo ‘“$number “$number” ” ” is treated literally inside ‘ ‘
echohello\t$name hello\themant Interpreted but by default escape sequence in off
echo -ehello\t$name hello    hemant Worked with -e option
\’ \’ \’ is interpreted inside ” “ but has no significance for


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads