shell - Bash: Append to list with variable name -
the first 3 commands work fourth not. how can append array variable in name?
i=4 eval pim$i= pim4+=(`date`) pim$i+=(`date`)
thanks!
with bash 4.3, there's feature targeted @ use case: namerefs, accessed declare -n
. (if have modern ksh
, they're available using nameref
built-in)
declare -n pim_cur="pim$i" pim_cur+=( "$(date)" )
with bash 4.2, can use printf -v
assign array elements:
array_len=${#pim4[@]} printf -v "pim4[$array_len]" %s "$(date)"
prior bash 4.2, may need use eval; can made safe using printf %q
preprocess data:
printf -v safe_date '%q' "$(date)" eval "pim$i+=( $safe_date )" # safe if can guarantee $i contain # characters valid inside variable name (such # numbers)
Comments
Post a Comment