I was trying to figure out env, ( i.e. calling a util with a new environment).
Just as an example my environment variable KDEDIRS = /usr in my current environment and lets say I type:
env -i KDEDIRS=/home/newkdedir env
This outputs KDEDIRS=/home/newkdedir as expected. (i.e calling second env with the new environment)
Now i wanna call say utility echo same way
env -i KDEDIRS=/home/new_kdedir echo ${KDEDIRS}
This is obviously not gonna work bec. shell expands KDEDIRS before it gets to echo. So the output is /usr (i.e. value in the current environment)
Then i try indirection and type in
env -i KDEDIRS=/home/newkdedir echo ${!KDEDIRS}
This outputs nothing.
I might be a little bit confused about this but how can i make the shell expand that KDEDIRS variable according to the newly created environment for echo?
Expansion happens as part of constructing the env command line which also sets the variable. No expansion is done within the execution of that command. So you must add another command line expander as part of that command. E.g.
Indirection has nothing to do with it.
Usually, you use `env' to give an environment to a command that it spawned off (eg. like you did with in your first snippet). Printing the variable back (that too using a shell builtin) might be possible through some perverse escaping and subshell tricks but it's not a very common use case (atleast not in my experience).