Describe the difference between single quotes (”) and double quotes (“”) in a shell script, and provide an example of when you would use each.

1 Answers
Answered by suresh

Shell Scripting: Difference Between Single Quotes and Double Quotes

Shell Scripting: Difference Between Single Quotes and Double Quotes

Single quotes ('') and double quotes ("") in a shell script serve different purposes:

  • Single Quotes (''): Enclosed text within single quotes is treated literally. Special characters, variables, and escape sequences are not interpreted inside single quotes. Use single quotes when you want to prevent variable expansion or special character interpretation.
  • Double Quotes (""): Enclosed text within double quotes allows for variable expansion and interpreting special characters and escape sequences. Use double quotes when you want to include variables or special characters within a string.

Examples:

Single Quotes Example:

#!/bin/bash
my_var="Hello"
echo 'This is a single quoted string with $my_var'

In this example, the output will be: This is a single quoted string with $my_var as the variable $my_var is not expanded within single quotes.

Double Quotes Example:

#!/bin/bash
my_var="Hello"
echo "This is a double quoted string with $my_var"

In this example, the output will be: This is a double quoted string with Hello as the variable $my_var is expanded within double quotes.

Answer for Question: Describe the difference between single quotes (”) and double quotes (“”) in a shell script, and provide an example of when you would use each.