Skip to main content
added 101 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

While you can use the indirect access as pointed in another answer, another way (in ksh and Bash 4.3 and newer) would be to use namerefs. Especially in the case of arrays this may be more useful since you can index the array through the nameref and don't need to put the index in the variable used as the reference.

arr1=(a b c)
arr2=(x y z)
typeset -n p=arr1    # or 'declare -n' 
echo "${p[1]}"       # prints 'b'

This doesn't work through the indirect access:

q=arr2
echo "${!q}"         # prints 'x', the same as $arr2
echo "${!q[1]}"      # doesn't work, it tries to take q[1] as a reference

but instead:

a='um' b='wait' c='what'
p=(a b c)
echo "${!p[2]}"      # prints 'what'

As a C programmer might put it, ${!q[1]} here acts as if q was an array of pointers, instead of being a pointer to an array.

While you can use the indirect access as pointed in another answer, another way (in ksh and Bash 4.3 and newer) would be to use namerefs. Especially in the case of arrays this may be more useful since you can index the array through the nameref and don't need to put the index in the variable used as the reference.

arr1=(a b c)
arr2=(x y z)
typeset -n p=arr1    # or 'declare -n' 
echo "${p[1]}"       # prints 'b'

This doesn't work through the indirect access:

q=arr2
echo "${!q}"         # prints 'x', the same as $arr2
echo "${!q[1]}"      # doesn't work, it tries to take q[1] as a reference

As a C programmer might put it, ${!q[1]} here acts as if q was an array of pointers, instead of being a pointer to an array.

While you can use the indirect access as pointed in another answer, another way (in ksh and Bash 4.3 and newer) would be to use namerefs. Especially in the case of arrays this may be more useful since you can index the array through the nameref and don't need to put the index in the variable used as the reference.

arr1=(a b c)
arr2=(x y z)
typeset -n p=arr1    # or 'declare -n' 
echo "${p[1]}"       # prints 'b'

This doesn't work through the indirect access:

q=arr2
echo "${!q}"         # prints 'x', the same as $arr2
echo "${!q[1]}"      # doesn't work, it tries to take q[1] as a reference

but instead:

a='um' b='wait' c='what'
p=(a b c)
echo "${!p[2]}"      # prints 'what'

As a C programmer might put it, ${!q[1]} here acts as if q was an array of pointers, instead of being a pointer to an array.

added 147 characters in body
Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

While you can use the indirect access as pointed in another answer, another way (in ksh and Bash 4.3 and newer) would be to use namerefs. Especially in the case of arrays, this may be more useful since you can index the array through the nameref and don't need to put the index in the variable used as the reference.

arr1=(a b c)
arr2=(x y z)
typeset -n p=arr1    # or 'declare -n' 
echo "${p[1]}"       # prints 'b'

This doesn't work through the indirect access:

q=arr2
echo "${!q}"         # prints 'x', the same as $arr2
echo "${!q[1]}"      # doesn't work, it tries to take q[1] as a reference

As a C programmer might put it, ${!q[1]} here acts as if q was an array of pointers, instead of being a pointer to an array.

While you can use the indirect access as pointed in another answer, another way would be to use namerefs. Especially in the case of arrays, since you can index the array through the nameref.

arr1=(a b c)
arr2=(x y z)
typeset -n p=arr1
echo "${p[1]}"       # prints 'b'

This doesn't work through the indirect access:

q=arr2
echo "${!q}"         # prints 'x', the same as $arr2
echo "${!q[1]}"      # doesn't work, it tries to take q[1] as a reference

As a C programmer might put it, ${!q[1]} here acts as if q was an array of pointers, instead of being a pointer to an array.

While you can use the indirect access as pointed in another answer, another way (in ksh and Bash 4.3 and newer) would be to use namerefs. Especially in the case of arrays this may be more useful since you can index the array through the nameref and don't need to put the index in the variable used as the reference.

arr1=(a b c)
arr2=(x y z)
typeset -n p=arr1    # or 'declare -n' 
echo "${p[1]}"       # prints 'b'

This doesn't work through the indirect access:

q=arr2
echo "${!q}"         # prints 'x', the same as $arr2
echo "${!q[1]}"      # doesn't work, it tries to take q[1] as a reference

As a C programmer might put it, ${!q[1]} here acts as if q was an array of pointers, instead of being a pointer to an array.

Source Link
ilkkachu
  • 147.9k
  • 16
  • 268
  • 441

While you can use the indirect access as pointed in another answer, another way would be to use namerefs. Especially in the case of arrays, since you can index the array through the nameref.

arr1=(a b c)
arr2=(x y z)
typeset -n p=arr1
echo "${p[1]}"       # prints 'b'

This doesn't work through the indirect access:

q=arr2
echo "${!q}"         # prints 'x', the same as $arr2
echo "${!q[1]}"      # doesn't work, it tries to take q[1] as a reference

As a C programmer might put it, ${!q[1]} here acts as if q was an array of pointers, instead of being a pointer to an array.