Книга: Fedora™ Unleashed, 2008 edition
Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
Using Double Quotes to Resolve Variables in Strings with Embedded Spaces
If a string contains embedded spaces, you can enclose the string in double quotes ("
) so that the shell interprets the whole string as one entity instead of more than one.
For example, if you assigned the value of abc def(abc
followed by one space, followed by def
) to a variable called x
in a shell program as follows, you would get an error because the shell would try to execute def
as a separate command:
Command | Environment |
---|---|
x=abc def |
bash |
set x=abc def |
tcsh |
The shell executes the string as a single command if you surround the string in double quotes, as follows:
Command | Environment |
---|---|
x="abc def" |
bash |
set x="abc def" |
tcsh |
The double quotes resolve all variables within the string. Here is an example for bash
:
var="test string"
newvar="Value of var is $var"
echo $newvar
If you execute a shell program containing the preceding three lines, you get the following result:
Value of var is test string
- Using X
- Using the Text Editors
- Using Digital Cameras with Fedora
- Using Backup Software
- Using Commands in the ftpaccess File to Configure wu-ftpd
- Using Fetchmail to Retrieve Mail
- Using DNS Tools
- Providing DNS for a Real Domain with BIND
- Using Perl with Linux
- Accessing Variable Values
- Using Single Quotes to Maintain Unexpanded Variables
- Using the Backtick to Replace a String with Output