The bash command recognizes five different quoting styles and each is different from the other. Knowing the difference will help you when composing a command.
" | Double quotes |
' | Single quotes |
\ | Escape quote or backslash |
[ctrl]v | Control quote |
` | Command Substitution or Back quotes (located on the ~ key) |
The double quote, ", is the basic quote. It is used to define a space delimited string as a single object. Double quotes quote spaces and tabs, single quotes, and most meta-characters such as filename wild cards. They will not interfere with the expansion of the variable reference $, and braces {}, the agrave `, the history reference !, or the escape quote \ when followed by a $, `, ", \, or the newline. You must always pair double quotes up.
Don't panic. As you use quotes, it will start to make sense.
The single quote, ', is also used to define a space delimited string as a single object. But unlike the double quotes, it quotes almost everything except control characters. You must always pair single quotes up.
It can also function like the control quote when combined with the variable reference and the escape quote. $'\b' is the embedded back space is the same as entering [ctr]v[ctrl]h. However if it is inside double quotes, it looses this functionality. See the bash man page for a list of recognized characters.
It is possible to concatenate alternately quoted strings to produce a single object as long as there are no spaces between each string.
The escape quote or backslash, \, quotes or preserves the literal value of the character immediately following it. If the character has a special meaning such as the variable reference, $, the history reference, !, or any of the filename meta-characters, it will loose its meaning. The one exception is the newline. If you end a line with a backslash, you may continue to enter text on the next line and the linebreak between the two lines will be discarded. that single quotes quote the \ and it loses its functionality.
Exercise : #( Enter the following : )
var="im a value" echo $var echo "$var" echo '$var' #( The following has a single quoted string containing a variable reference followed by a double quoted string containing a single quote and a variable reference. )
echo '$var'"'s contents : $var"#( The following uses the escape quote to quote just the variable reference character.)
echo "\$var's contents : $var"#( Try the following with the escape quote or backslash. )
echo "one line \ message" echo "this is not" echo 'neither \ is this' |
The control quote, [ctrl]v, allows you to quote a control character. The other quote characters are evaluated after the command line has been entered but before the command is executed. The control quote is processed as the character is received from the terminal interface and before the character is placed in the command line buffer. The other quotes have no affect on the control quote.
The final quote we will look at is the backquote or agrave, `. This quote performs a special action called command substitution. When a string is placed between two backquotes and the command line is processed, the string will be treated as if it were its own command sequence. The shell will attempt to run it and place its output back on the command line before running the main command.
Command substitution is often used with variables.
We wish to perform a calculation on a numeric value stored in a variable. We can use expr to perform the calculation. But expr sends output to standard out. If we redirect the output then we have the new value in a file not a variable. With command substitution, we can run expr and place its output in the command line buffer as an argument to assigning a value to a variable.
Exercise : #( Enter the following : )
qtnt="93" dvsr="4" expr $qtnt / $dvsr expr $qtnt % $dvsr #( Use command substitution to capture output back into the command line. ) dvdd=`expr $qtnt / $dvsr` rmdr=`expr $qtnt % $dvsr` echo "Dividend $dvdd remainder $rmdr" |
Some things about command substitution :
The last feature can be useful when defining an alias. Lets say you want an alias that called when that will set a variable when to the current time when invoked. If we define the alias as :
alias When="when=`date`"
and we run
alias When
Displays :
alias When='when=Mon Jul 9 14:35:14 CDT 2007'
We wsee that the date is already stored in the alias definition which is not what we want. We want the command substitution and command stored in the definition.
If we use singe quotes insteadd :
alias When='when=`date`'
Now run the following :
alias When
When
echo $when
When
echo $when
You should notice the time changing each time you invoke the When alias and display the contents of the when variable.
One mechanism for defining and recalling a command sequence is the alias.