Shell Script to Show the Difference Between echo “$SHELL” and echo ‘$SHELL’
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.
Single quote Interpretation:
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:
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"
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:
Below is the table to understand the behavior of single and double quotes. First, we initialize some variables.
# name = (hemant) # number = 40
|echo “$name“||hemant||Variable get interpreted|
|echo ‘$name‘||$name||Literal meaning get printed|
|echo ‘$number‘||$number||No interpretation of variable|
|echo ‘“$number“‘||“$number”||” ” is treated literally inside ‘ ‘|
|echo “hello\t$name“||hello\themant||Interpreted but by default escape sequence in off|
|echo -e “hello\t$name“||hello hemant||Worked with -e option|
|“\’“||\’||\’ is interpreted inside ” “ but has no significance for ‘|