Skip to content
Snippets Groups Projects
Commit d502fce1 authored by Thomas Junier's avatar Thomas Junier
Browse files

indirect expansion, more about arrays

parent f19c22b9
Branches
No related tags found
No related merge requests found
......@@ -122,11 +122,22 @@ Parameter Expansion
\begin{tabular}{p{2.0cm}p{6cm}}
\textbf{expansion} & \textbf{value} \\
\texttt{\$\{var\}} & value of \texttt{var} (as a string) \\
\texttt{\$\{{\itshape var}\}} & value of \texttt{var} (as a string) \\
\texttt{\$var} & short for \texttt{\$\{var\}}, ok iff not prefix of longer word \\
\texttt{\$\{\#var\}} & string length of \texttt{\$var} \\
\end{tabular}
### Indirect Expansion
Variables can hold the names of other variables.
\begin{tabular}{p{2.0cm}p{6cm}}
\texttt{\$\{!{\itshape name}\}} & If \texttt{\itshape name} is a \textit{nameref}: expand to the name of the referenced variable.\\
\texttt{\$\{!{\itshape name}\}} & If \texttt{\itshape name} is not a
\textit{nameref}: take \texttt{\${\itshape name}} to be a parameter name, and
expand it (TVCA).\\
\end{tabular}
### Handling null and unset
A parameter is _null_ if its value is the empty string. It is _unset_ if it has _no value_. \
......@@ -577,17 +588,35 @@ Creation and Assignment
Retrieval
---------
\texttt{\itshape sub} below ("subscript") stands for either an integer or a string,
for indexed or associative arrays, respectively.
\begin{tabular}{p{3.0cm}p{5.5cm}}
\textbf{expression} & \textbf{value} \\
\texttt{\${\itshape array}[{\itshape subscript]}} & the value of array at index
or key \texttt{{\itshape subscript}}. \\
\texttt{\${\itshape a}[@]}, \texttt{\${\itshape a}[*]} & \texttt{\${\itshape a}[1] ... \${\itshape a}[n]} \\
\texttt{"\${\itshape a}[*]"} & \texttt{"\${\itshape a}[1]s\${\itshape a}[2]s...\${\itshape a}[n]"}; separator \texttt{s} is the 1\textsuperscript{st} char of \texttt{\$IFS} (\texttt{space} if unset, empty if null) \\
\texttt{"\${\itshape a}[@]"} & \texttt{"\${\itshape a}[1]" "\${\itshape a}[2]" ... "\${\itshape a}[n]"} \\
\\
\texttt{\$\{{\itshape array}[{\itshape sub}]\}} & the value of array \texttt{\itshape array} at index or key \texttt{{\itshape sub}}. \\
\texttt{\$\{{\itshape a}[@]\}}, \texttt{\$\{{\itshape a}[*]\}} & \texttt{\$\{{\itshape a}[1]\} ... \$\{{\itshape a}[n]\}} \\
\texttt{"\$\{{\itshape a}[*]\}"} & \texttt{"\$\{{\itshape a}[1]\}s\$\{{\itshape a}[2]\}s...\$\{{\itshape a}[n]\}"}; separator \texttt{s} is the 1\textsuperscript{st} char of \texttt{\$IFS} (\texttt{space} if unset, empty if null) \\
\texttt{"\$\{{\itshape a}[@]\}"} & \texttt{"\$\{{\itshape a}[1]\}" "\$\{{\itshape a}[2]\}" ... "\$\{{\itshape a}[n]\}"} \\
\texttt{"\$\{\#{\itshape array}[{\itshape sub}]\}"} & length of \texttt{"\$\{{\itshape array}[{\itshape sub}]\}"} \\
\texttt{\$\{\#{\itshape array}[{\itshape sub}]\}} & length of \texttt{\$\{{\itshape array}[{\itshape sub}]\}} \\
\texttt{\$\{\#{\itshape a}[@]\}}, \texttt{\$\{\#{\itshape a}[*]\}} & number of elements in \texttt{\itshape{a}}. \\ %*
\texttt{\$\{!{\itshape a}[@]\}}, \texttt{\$\{!{\itshape a}[*]\}} & like \texttt{\$\{{\itshape a}[@]\}}, but with the \textit{subscripts} instead of the values \\
%\texttt{"\$\{!{\itshape a}[*]\}"} & like \texttt{"\$\{{\itshape a}[*]\}"}, but with the subscripts \\
%\texttt{"\$\{!{\itshape a}[@]\}"} & like \texttt{"\$\{{\itshape a}[@]\}"}, but with the subscripts \\
\end{tabular}
Destruction
-----------
\begin{tabular}{p{3.0cm}p{5.5cm}}
\textbf{expression} & \textbf{value} \\
\texttt{unset {\itshape a}[{\itshape sub}]} & removes the element at index or key \texttt{{\itshape sub}} from array \texttt{\itshape a}. \\
\texttt{unset {\itshape a}} & removes the array \texttt{\itshape a}. \\
\texttt{unset {\itshape a}[@]} & removes the array \texttt{\itshape a}. \\
\texttt{unset {\itshape a}[*]} & removes the array \texttt{\itshape a}. \\
\end{tabular}
Functions
=========
......@@ -625,8 +654,7 @@ TODO
* `eval` (and maybe `expr`)
* `declare`
* namerefs
* arrays
* namerefs (`declare -n`)
* idioms: read x y < <(...) (use process substitution to set variables)
* f -o a1 a2 -- you can pass switches and options to functions
* regexps `[[ $var =~ regexp ]]`
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment