Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

Click here to view code image
Value of var is $var


As you can see, the variable var maintains its original format in the results
rather than being expanded.


Using the Backslash as an Escape Character

As you learned earlier, the backslash () serves as an escape character that
stops the shell from interpreting the succeeding character as a special
character. Say that you want to assign the value $test to a variable called
var. If you use the following command, the shell reads the special character
$ and interprets $test as the value of the variable test. No value has been
assigned to test; a null value is stored in var as follows:


Command Environment

var=$test pdksh and bash

set var=$testtcsh

Unfortunately, this assignment might work for bash and pdksh, but it
returns an “undefined variable” error if you use it with tcsh. Use the
following commands to correctly store $test in var:


Command Environment

var=\$test pdksh    and bash

set var =   \$testtcsh

The backslash before the dollar sign (\$) signals the shell to interpret the $ as
any other ordinary character and not to associate any special meaning to it.
You could also use single quotes (’) around the $test variable to get the
same result.


Using the Backtick to Replace a String with Output

You can use the backtick (`) character to signal the shell to replace a string
with its output when executed. This is called command substitution. You can
use this special character in shell programs when you want the result of the

Free download pdf