There are three types of quotes used by the Bash shell: single quotes ('), double quotes (") and back quotes (`). These quotes have special features in the Bash shell as described below.
To understand single and double quotes, consider that there are times that you don't want the shell to treat some characters as "special". For example, as you learned earlier in this lab, the *character is used as a wildcard. What if you wanted the * character to just mean an asterisk?
Single quotes prevent the shell from "interpreting" or expanding all special characters. Often single quotes are used to protect a string from being changed by the shell, so that the string can be interpreted by a command as a parameter to affect the way the command is executed.
Double quotes stop the expansion of glob characters like the asterisk (*), question mark (?), and square brackets ( [ ] ). Double quotes do allow for both variable expansion and command substitution (see back quotes) to take place.
Back quotes cause "command substitution" which allows for a command to be executed within the line of another command.
When using quotes, they must be entered in pairs or else the shell will not consider the command complete.
While single quotes are useful for blocking the shell from interpreting one or more characters, the shell also provides a way to block the interpretation of just a single character called "escaping" the character. To "escape" the special meaning of a shell metacharacter, the backslash "\" character is used as a prefix to that one character.
Execute the following command to use back quotes (`) to execute the date
command within the line of the echo
command:
echo Today is `date`
Your output should be similar to the following:
You can also place $( before the command and ) after the command to accomplish command substitution:
echo Today is $(date)
Your output should be similar to the following:
Why two different methods that accomplish the same thing? Backquotes look very similar to single quotes, making it harder to "see" what a command is supposed to do. Originally shells used backquotes; the $(command) format was added in a later version of the Bash shell to make the statement more visually clear.
If you don't want the backquotes to be used to execute a command, place single quotes around them. Execute the following:
echo This is the command '`date`'
Your output should be similar to the following:
Note that you could also place a backslash character in front of each backquote character. Execute the following:
echo This is the command \`date\`
Your output should be similar to the following:
Double quote characters don't have any effect on backquote characters. The shell will still use them as command substitution. Execute the following to see a demonstration:
echo This is the command "`date`"
Your output should be similar to the following:
Double quote characters will have an effect on wildcard characters, disabling their special meaning. Execute the following:
echo D*
echo "D*"
Your output should be similar to the following: