Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: is variable with variable name possible?



On Apr 4,  3:51pm, Jeremie Roquet wrote:
} Subject: Re: is variable with variable name possible?
}
} 2011/4/4 Jeremie Roquet <arkanosis@xxxxxxxxx>:
} > 2011/4/4 zzapper <david@xxxxxxxxxx>:
} >> somevar=$(pwd)
} >>
} >> but I want somevar to be a variable name
} >
} > $ eval "$somevar=$(pwd)"
} 
} Actually, the quotes aren't even needed:
} 
} $ eval $somevar=$(pwd)

If $(pwd) has spaces in the directory name, this is going to fail.  So
you do need quotes, but a different kind:

    eval $somevar='$(pwd)'

For scalars (but not array assignments) you can also do

    typeset -g $somevar=$(pwd)

You can also use ${somevar::=$(pwd)} anywhere that you'd reference the
value of $somevar, including the no-op command:

    : ${somevar::=$(pwd)}

That also works for array assignments if you explicitly split:

    : ${(As:/:)somevar::=$(pwd)}
    print -l $somevar

Note though that here the stuff after the = is joined with the first
character of $IFS before being split, so to copy one array to another
you have to be careful.  Also note that without the (A) the split is
applied to the expansion after the assignment.



Messages sorted by: Reverse Date, Date, Thread, Author