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

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.
The single quote will not interpret anything like variables, backslash, etc. into other forms. For example:
# h = 5
# echo '$h'
$: $ 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'
Even though the variable SHELL contains the path of the shell it did not print the path on output because we used single quote ‘ ‘.
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.
Write content to file:
h = 5
echo '$h'
Press ctrl + o to save and press ctrl + x to exit, now give permission to program.sh file to execute:
# chmod 777 program.sh
Run the file:
# ./program.sh
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"
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"
Now let us try the whole thing using a shell script file.
Open a file name newProgram.sh using nano editor:
# nano newProgram.sh
Write the content given below:
h = 5
echo "$h"
Give permission to make it executable:
# chmod +x newProgram.sh
Run the file to see the output:
# ./newProgram.sh
Below is the table to understand the behavior of single and double quotes. First, we initialize some variables.
# name = (hemant)
# number = 40
Kali-Linux
Picked
Difference Between
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Similarities and Difference between Java and C++
Difference between Compile-time and Run-time Polymorphism in Java
Differences and Applications of List, Tuple, Set and Dictionary in Python
Sed Command in Linux/Unix with examples
AWK command in Unix/Linux with examples
grep command in Unix/Linux
cut command in Linux with examples
cp command in Linux with examples