arr$varEnvCol[$index]="$(...)"
doesn’t work the way you expect it to – you cannot assign to shell variables indirectly – via an expression that expands to the variable name – this way.
Your attempted workaround with eval
is also flawed – see below.
tl;dr
If you use bash 4.3 or above:
declare -n targetArray="arr$varEnvCol"
targetArray[index]=$(echo $line | awk -F, '{print $1}')
bash 4.2 or earlier:
declare "arr$varEnvCol"[index]="$(echo $line | awk -F, '{print $1}')"
Caveat: This will work in your particular situation, but may fail subtly in others; read on for details, including a more robust, but cumbersome alternative based on read
.
The eval
-based solution mentioned by @shellter in a since-deleted comment is problematic not only for security reasons (as they mentioned), but also because it can get quite tricky with respect to quoting; for completeness, here’s the eval
-based solution:
eval "arr$varEnvCol[index]"='$(echo $line | awk -F, '\''{print $1}'\'')'
See below for an explanation.
Assign to a bash
array variable indirectly:
bash 4.3+
: use declare -n
to effectively create an alias (‘nameref’) of another variable
This is by far the best option, if available:
declare -n targetArray="arr$varEnvCol"
targetArray[index]=$(echo $line | awk -F, '{print $1}')
declare -n
effectively allows you to refer to a variable by another name (whether that variable is an array or not), and the name to create an alias for can be the result of an expression (an expanded string), as demonstrated.
bash 4.2-
: there are several options, each with tradeoffs
NOTE: With non-array variables, the best approach is to use printf -v
. Since this question is about array variables, this approach is not discussed further.
- [most robust, but cumbersome]: use
read
:
IFS=$'\n' read -r -d '' "arr$varEnvCol"[index] <<<"$(echo $line | awk -F, '{print $1}')"
IFS=$'\n'
ensures that that leading and trailing whitespace in each input line is left intact.-r
prevents interpretation of\
chars. in the input.-d ''
ensures that ALL input is captured, even multi-line.- Note, however, that any trailing
\n
chars. are stripped. - If you’re only interested in the first line of input, omit
-d ''
- Note, however, that any trailing
"arr$varEnvCol"[index]
expands to the variable – array element, in this case – to assign to; note that referring to variableindex
inside an array subscript does NOT need the$
prefix, because subscripts are evaluated in arithmetic context, where the prefix is optional.-
<<<
– a so-called here-string – sends its argument tostdin
, whereread
takes its input from. -
[simplest, but may break]: use
declare
:
declare "arr$varEnvCol"[index]="$(echo $line | awk -F, '{print $1}')"
- (This is slightly counter-intuitive, in that
declare
is meant to declare, not modify a variable, but it works in bash 3.x and 4.x, with the constraints noted below.) - Works fine OUTSIDE a FUNCTION – whether the array was explicitly declared with
declare
or not. -
Caveat: INSIDE a function, only works with LOCAL variables – you cannot reference shell-global variables (variables declared outside the function) from inside a function that way. Attempting to do so invariably creates a LOCAL variable ECLIPSING the shell-global variable.
-
[insecure and tricky]: use
eval
:
eval "arr$varEnvCol[index]"='$(echo $line | awk -F, '\''{print $1}'\'')'
- CAVEAT: Only use
eval
if you fully control the contents of the string being evaluated;eval
will execute any command contained in a string, with potentially unwanted results. - Understanding what variable references/command substitutions get expanded when is nontrivial – the safest approach is to delay expansion so that they happen when
eval
executes rather than immediate expansion that happens when arguments are passed toeval
. - For a variable assignment statement to succeed, the RHS (right-hand side) must eventually evaluate to a single token – either unquoted without whitespace or quoted (optionally with whitespace).
- The above example uses single quotes to delay expansion; thus, the string passed mustn’t contain single quotes directly and thus is broken into multiple parts with literal
'
chars. spliced in as\'
. - Also note that the LHS (left-hand side) of the assignment statement passed to
eval
must be a double-quoted string – using an unquoted string with selective quoting of$
won’t work, curiously:- OK:
eval "arr$varEnvCol[index]"=...
- FAILS:
eval arr\$varEnvCol[index]=...
- OK: