aboutsummaryrefslogtreecommitdiffhomepage
path: root/doc_src
diff options
context:
space:
mode:
authorGravatar Jak Wings <jakwings@gmail.com>2015-12-13 11:13:15 +0800
committerGravatar Jak Wings <jakwings@gmail.com>2015-12-14 02:36:00 +0800
commitaa974b58bc6d4919c6bfd9336f420e5303c4b12a (patch)
treec8fb116d949f9427eb7914f3df2f359af569ae2d /doc_src
parent8104854d5dc2ef22526558e3139e47ce56f4e02e (diff)
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.
Diffstat (limited to 'doc_src')
-rw-r--r--doc_src/index.hdr.in12
1 files changed, 7 insertions, 5 deletions
diff --git a/doc_src/index.hdr.in b/doc_src/index.hdr.in
index 34b25755..1b28e140 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 <a href="#expand-brace">brace expansion</a>; 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 <a href="#expand-brace">brace expansion</a>; 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: