Книга: 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

Оглавление книги


Генерация: 0.578. Запросов К БД/Cache: 3 / 0
поделиться
Вверх Вниз