Linux Protecting Arguments from Expansion

Many characters have special meaning in the Bash shell. To ignore meta-character special
meanings, quoting and escaping are used to protect them from shell expansion. The backslash
(\) is an escape character in Bash, protecting the single following character from special
interpretation. To protect longer character strings, single ( ') or double quotes (") are used to
enclose strings.

Use double quotation marks to suppress globbing and shell expansion, but still allow command
and variable substitution. Variable substitution is conceptually identical to command
substitution, but may use optional brace syntax.

host=$(hostname); echo $host

echo "***** hostname is ${host} *****"

echo Your username variable is \$USER.

Linux Protecting Arguments from Expansion

Use single quotation marks to interpret all text literally. Observe the difference, on both screeen
and keyboard, between the single quote (') and the command substitution back-tick (`). Besides
suppressing globbing and shell expansion, quotations direct the shell to additionally suppress
command and variable substitution. The question mark is a meta-character that also needed
protection from expansion.

echo "Will variable $host evaluate to $(hostname)?"

echo 'Will variable? $host evaluate to $(hostname).'

Linux Arguments




Tech info

Comments