0

I am trying to substitute a string variable (an array here), but I am getting an error. Could anyone suggest how to solve this?

COMP="MY"

MY_common_sections_to_fix=( \
      ".rodata" \
      ".data" \
      )

 echo ${${COMP}_common_sections_to_fix[@]}

ERROR:

${${COMP}_common_sections_to_fix[@]}: bad substitution

4
  • Which version of bash? There's a convenient builtin available in 4.4. Commented Jun 21, 2017 at 0:43
  • Since the array you initialized is MY_common_sections_to_fix but you are trying to access L1CC_common_sections_to_fix, that's one level of non-surprise. However, the dynamic naming of variables is also problematic. You could create a variable holding MY_common_sections_to_fix and work out how to exploit ${!varname[@]} or thereabouts — untested, but ${!var} references the variable with the name stored in $var. (Semi-tested: the results do not look promising. Assume that it won't work until further notice!) Commented Jun 21, 2017 at 0:45
  • Further notice: it won't work — see shell parameter expansion in the Bash manual. It looks like you can't do indirect expansion of array names. Commented Jun 21, 2017 at 0:54
  • BashFAQ #6 goes into which tricks for indirect reference are available on which shells, and which apply to arrays. Note the caveats given attached to the last example in the section as an example of how error-prone doing this in a shell without the pertinent features can be. Commented Jun 21, 2017 at 0:54

1 Answer 1

2

What you're trying to do is indirectly reference an array.

A nameref, if you have a new enough shell (4.3 or later), is the most appropriate tool for the job:

COMP=MY
MY_common_sections_to_fix=( .rodata .data )
declare -n active_sections=${COMP}_common_sections_to_fix
printf '%s\n' "${active_sections[@]}"

This makes active_sections an alias for MY_common_sections_to_fix, and properly emits .rodata and .data as output.


If you don't have bash 4.3 available, hackery with eval is an available (albeit unfortunate) option:

printf -v cmd '%q=( "${%q[@]}" )' active_sections "${COMP}_common_sections_to_fix"
eval "$cmd"
Sign up to request clarification or add additional context in comments.

4 Comments

Eh, they used "MY" in the variable name in the question. They use both values, that means either is equally correct -- one or the other needs to be modified to generate a working example.
I thought they were trying to make it L1CC_common_sections_to_fix, but maybe not. The first command works with 3.x if I take out the -n option.
@l'L'l, eh? In 3.x, I get declare -- active_sections="MY_common_sections_to_fix" as the result if the -n is removed -- meaning only the name of the target variable, and not its value, is available.
Actually I have no clue what the OP is trying to do, so +1 for trying to make any sense from it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.