From aa974b58bc6d4919c6bfd9336f420e5303c4b12a Mon Sep 17 00:00:00 2001 From: Jak Wings Date: Sun, 13 Dec 2015 11:13:15 +0800 Subject: [PATCH] Doc: Introduce another simple way to separate variable names from text. This can avoid the confusion between brace expansion and the cartesian product behavior of arrays, even if braces can help to do some hacks. --- doc_src/index.hdr.in | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/doc_src/index.hdr.in b/doc_src/index.hdr.in index 34b257553..1b28e1408 100644 --- a/doc_src/index.hdr.in +++ b/doc_src/index.hdr.in @@ -460,7 +460,7 @@ A dollar sign followed by a string of characters is expanded into the value of t Undefined and empty variables expand to nothing. -To separate a variable name from text it should immediately be followed by, encase the variable within braces. +To separate a variable name from text it should immediately be followed by, encase the variable within quotes. Examples: \fish @@ -470,15 +470,17 @@ echo $HOME echo $nonexistentvariable # Prints no output. -echo The plural of $WORD is {$WORD}s +echo The plural of $WORD is "$WORD"s # Prints "The plural of cat is cats" when $WORD is set to cat. +echo The plural of $WORD is {$WORD}s +# ditto \endfish -Note that without the braces, fish will try to expand a variable called `$WORDs`, which may not exist. +Note that without the quotes or braces, fish will try to expand a variable called `$WORDs`, which may not exist. -The latter syntax works by exploiting brace expansion; care should be taken with array variables and undefined variables, as these behave very differently to POSIX shells. +The latter syntax `{$WORD}` works by exploiting brace expansion; care should be taken with array variables and undefined variables, as these behave very differently to POSIX shells. -Variable expansion is the only type of expansion performed on double quoted strings. There is, however, an important difference in how variables are expanded when quoted and when unquoted. An unquoted variable expansion will result in a variable number of arguments. For example, if the variable `$foo` has zero elements or is undefined, the argument `$foo` will expand to zero elements. If the variable $foo is an array of five elements, the argument `$foo` will expand to five elements. When quoted, like `"$foo"`, a variable expansion will always result in exactly one argument. Undefined variables will expand to the empty string, and array variables will be concatenated using the space character. The dangers noted in the third example above can therefore be avoided by wrapping the variable in double quotes (`echo {"$WORD"}s`). +Variable expansion is the only type of expansion performed on double quoted strings. There is, however, an important difference in how variables are expanded when quoted and when unquoted. An unquoted variable expansion will result in a variable number of arguments. For example, if the variable `$foo` has zero elements or is undefined, the argument `$foo` will expand to zero elements. If the variable $foo is an array of five elements, the argument `$foo` will expand to five elements. When quoted, like `"$foo"`, a variable expansion will always result in exactly one argument. Undefined variables will expand to the empty string, and array variables will be concatenated using the space character. There is one further notable feature of fish variable expansion. Consider the following code snippet: