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 a value of $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=$test tcsh

Unfortunately, this assignment may work for bash and pdksh, but it returns an error of "undefined variable" 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 = tcsh

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.

Was this article helpful?

0 0

Post a comment