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

Re: Avoid duplication of code



> 2020/05/25 18:02, Yasuhiro KIMURA <yasu@xxxxxxxxxxx> wrote:
> 
> 1. Define function that updates single environment variable and pass
>   variable name as argument of it.
> 2. Use 'for name in word; do list done' such as following.
> 
>      for valname in SSH_AUTH_SOCK SSH_AGENT_PID
>      do
>        (update value of environment variable named ${valname})
>      done
> 
> But it seems that both require nested variable expansion and that it
> is impossible. Then is there any other way to avoid code duplication?

Suppose a parameter, say valname, contains a name of another parameter;

valname=SSH_AUTH_SOCK

Then you can GET the current value of SSH_AUTH_SOCK by

if [[ -n ${(P)valname} ]]; then ...

You can SET a new value to SSH_AUTH_SOCK in a couple of ways.
In your case, probably

export $valname='new value'

would be the simplest. If you don't want to export the variable, then,

: ${(P)valname::=new value}




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