Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1
set x=“abc  def”tcsh

The double quotes resolve all variables within the string. Here is an example
for pdksh and bash:


Click here to view code image
var="test string"
newvar="Value of var is $var"
echo $newvar


Here is the same example for tcsh:


Click here to view code image
set var="test string"
set newvar="Value of var is $var"
echo $newvar


If you execute a shell program containing the preceding three lines, you get
the following result:


Click here to view code image
Value of var is test string


Using Single Quotes to Maintain Unexpanded Variables

You can surround a string with single quotes (’) to stop the shell from
expanding variables and interpreting special characters. When used for the
latter purpose, the single quote is an escape character, similar to the
backslash, which you learn about in the next section. Here, you learn how to
use the single quote to avoid expanding a variable in a shell script. An
unexpanded variable maintains its original form in the output.


In the following examples, the double quotes from the preceding examples
have been changed to single quotes:


Click here to view code image
pdksh and bash:
var='test string'
newvar='Value of var is $var'
echo $newvar
tcsh:
set var = 'test string'
set newvar = 'Value of var is $var'
echo $newvar


If you execute a shell program containing these three lines, you get the
following result:

Free download pdf