From 811dbf0f9acf328d43c5b12cc0853de5085a4d0c Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Wed, 15 Feb 2023 18:29:14 +0100 Subject: [PATCH] docs: More on dereferencing variables Also that unclosed quote was driving me up the wall --- doc_src/language.rst | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/doc_src/language.rst b/doc_src/language.rst index 16558cc88..51862c6a3 100644 --- a/doc_src/language.rst +++ b/doc_src/language.rst @@ -693,10 +693,29 @@ The ``$`` symbol can also be used multiple times, as a kind of "dereference" ope # 20 # 30 -``$$foo[$i]`` is "the value of the variable named by ``$foo[$i]``. +``$$foo[$i]`` is "the value of the variable named by ``$foo[$i]``". When using this feature together with list brackets, the brackets will be used from the inside out. ``$$foo[5]`` will use the fifth element of ``$foo`` as a variable name, instead of giving the fifth element of all the variables $foo refers to. That would instead be expressed as ``$$foo[1..-1][5]`` (take all elements of ``$foo``, use them as variable names, then give the fifth element of those). +Some more examples:: + + set listone 1 2 3 + set listtwo 4 5 6 + set var listone listtwo + + echo $$var + # Output is 1 2 3 4 5 6 + + echo $$var[1] + # Output is 1 2 3 + + echo $$var[2][3] + # $var[1] is listtwo, third element of that is 6, output is 6 + + echo $$var[..][2] + # The second element of every variable, so output is + # 2 5 + .. _expand-command-substitution: Command substitution