expression

Description

Expression is a string consisting of unquoted characters other than whitespace or semicolon, single quote or double quote characters or quoted characters of any kind (but see also special case of expression - so called here-documents - described below). Quoting means either preceding a single character with a backslash or enclosing a part of the string into single quotes '...' or double quotes "...". Quoting characters are removed from the string so they must be quoted themselves if they are a part of the expression: \\, \' or " ' ", \" or ' " '. Unquoted (sub)expressons and (sub)expressions quoted with double-quotes are subject to variable, Perl, and XPath expansions.

Variable expansion replaces substrings of the form $id or ${id} with the value of the variable named $id, unless the '$' sign is quoted.

Perl expansion evaluates every substring enclosed in between ${{{ and }}} as a Perl expresson (in very much the same way as the perl command) and replaces the whole thing with the resulting value.

XPath interpolation evaluates every substring enclosed in between ${{ and }} as an XPath expression (in very much the same way as the count command) and substitutes the whole thing with the resul.

For convenience, another kind XPath interpolation is performed on expressions. It replaces any substring occuring between ${( and )} with a literal result of XPath evaluation of the string. This means, that if the evaluation results in a node-list, the textual content of its first node is substituted rather than the number of nodes in the node-list (as with ${{ ... }}).

echo foo "bar"                        # prints: foo bar
echo foo"bar"                         # prints: foobar
echo foo'"bar"'                       # prints: foo"bar"
echo foo"'b\\a\"r'"                   # prints: foo'b\a"r'
$a="bar"
echo foo$a                            # prints: foobar
echo foo\$a                           # prints: foo$a
echo '$a'                             # prints: '$a'
echo "'$a'"                           # prints: 'bar'
echo "${{//middle-earth/creatures}}"  # prints: 10
echo '${{//middle-earth/creatures}}'  # prints: ${{//middle-earth/creatures}}
echo ${{//creature[1]/@name}}         # !!! prints: 1
echo ${(//creature[1]/@name)}         # prints: Bilbo
echo ${{{ join(",",split(//,$a)) }}}  # prints: b,a,r

There is one more special type of expressions, so called ``here-documents'' following syntax of similar constructs in Bash and Perl. Following a << you specify a string to terminate the quoted material, and all lines following the current line down to the terminating string are the value of the expression. The terminating string is either quoted or unquoted identifier (a word). If quoted, the type of quotes you use determines the treatment of the text, just as in regular quoting, i.e. in case of double quotes, the material contained in the here-document is subject to variable, Perl, and XPath expansions. An unquoted identifier works just like double quotes. There must be no space between the << and the identifier. The terminating string must appear by itself (unquoted and with no surrounding whitespace) on the terminating line.

$a="bar"
echo foo <<END baz;
xx $a yy
END
# prints foo xx bar yy baz
echo foo <<"END" baz;
xx $a yy
END
# same as above
echo foo <<'END' baz;
xx $a yy
END
# prints foo xx $a yy baz