[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] indirection for arrays

From: John Kearney
Subject: Re: [Help-bash] indirection for arrays
Date: Sat, 05 May 2012 22:35:39 +0200
User-agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20120428 Thunderbird/12.0.1

Am 05.05.2012 22:16, schrieb Bill Gradwohl:
Via a web site reference, Dan Douglas (Thank You) provided something akin to this:

func() {
    local -a 'keys=("${!'"$1"'address@hidden")' 

declare -a myArray=(1 2 3)

func 'myArray'

Neat trick! Took me a while to figure out how it works. The single quotes in particular stopped me in my tracks for a while.

Can I rely on this in future versions of bash, or is this a side effect that might disappear some day? I can find no documentation that says specifically it's supported.

Also, since indirection requires the ! immediately after a {, this means indirection can never be on the left hand side of an assignment. Correct?

The "trick" above is doing a type of indirection by getting the parser to cooperate in a sneaky fashion. At least that's my analysis.

Is there some other mechanism ("trick") besides eval that can do indirection on the left hand side of an assignment? A mechanism that can be relied on in future releases.

Bill Gradwohl

off the top of my head you can also do this sort of thing.
assignment is easier than recovery.

since something like bash 3.1 ish
printf -v "${1}"  "%s"   "Value"
printf -v "${1}[1]"  "%s"   "Value"

since something like bash 4.2 ish,
declare -g "${1}=jkjoiij"

again if your working with integers
: $(( ${1}[1]=3 ))
let "${1}[1]=4"

View John
          Kearney's profile on LinkedIn John Kearney

reply via email to

[Prev in Thread] Current Thread [Next in Thread]