Jump to navigation Jump to search
120 bytes added ,  22:49, 29 March 2016
Text replacement - "<source lang="bash">" to "<syntaxhighlight lang="bash" >"
Your bash shell understands special characters with special meanings. For example, $var is used to expand the variable value. Bash expands variables and [[wildcards]], for example:
<source syntaxhighlight lang="bash">echo "$PATH"
echo "$PS1"
echo /etc/*.conf</source>
However, sometime you do not wish to use variables or [[wildcards]]. For example, do not print value of $PATH, but just print $PATH on screen as a word. You can enable or disable the meaning of a special character by enclosing them in single quotes. This is also useful to suppress warnings and error messages while writing the shell scripts.
<source syntaxhighlight lang="bash">echo "Path is $PATH" ## $PATH will be expanded</source>
<source syntaxhighlight lang="bash">echo 'I want to print $PATH' ## PATH will not be expanded</source>
There are three types of quotes:
==The Backslash==
The backslash ( \ ) alters the special meaning of the ' and " i.e. it will escape or cancel the special meaning of the next character. The following will display filename in double quote:
<source syntaxhighlight lang="bash">FILE="/etc/resolv.conf"
echo "File is \"$FILE\" "</source>
Sample Outputs:
<pre>File is "/etc/resolv.conf"</pre>
The following will remove the special meaning of the dollar ( $ ) sign:
<source syntaxhighlight lang="bash">FILE="/etc/resolv.conf"
echo "File is \$FILE "</source>
Sample Outputs:
<source syntaxhighlight lang="bash">echo "Pizza bill \$22.5"
echo -e "\a Ding dong\a"
echo "CIFS path must be \\\\NT-Server-Name\\ShareName"
echo -e "2\t Dragon Ball Z (INR.300)"</source>
The special parameters * and @ have special meaning when in double quotes, but you can disable them with the backslash:
<source syntaxhighlight lang="bash">echo "*"
echo "\*"
echo "\@"</source>
====Continue command on next line====
You can use the backslash (\) as last character on line to continue command on next line:
<source syntaxhighlight lang="bash">echo "A monkey-tailed boy named Goku is found by an old martial \
>arts expert who raises him as his grandson. One day Goku meets a \
>girl named Bulma and together they go on a quest to retrieve the seven Dragon Balls"</source>
You can also use the backslash while writing program or [[Writing_your_first_shell_function|function]]:
<source syntaxhighlight lang="bash"># Purpose: clean /tmp/$domain ?
check_temp_clean() {
[ "$SERVER_MODE" = "daemon" ] || return 1
====Protecting command line arguments====
Type the following command to find out all c program file (*.c):
<source syntaxhighlight lang="bash">find $HOME -name *.c</source>
In the above example, the *.c is expanded by the bash shell. It will try to match all filename ending with .c in the /home directory (current user's home directory) such as main.c, lib1.c, lib2.c, ssh.c, auth.c etc. You can escape the wild card using the backslash as the escape character:
<source syntaxhighlight lang="bash">find $HOME -name \*.c
find $HOME -name \*main.c
find /nas01/apps/admin -iname \*py </source>
You can also use the double quote
<source syntaxhighlight lang="bash">find $HOME -name "*.c"</source>
[[Category:Variables and Quoting]]

Navigation menu