JLB JLB - 1 year ago 61
Bash Question

How to dynamically compute value of a variable and use it in a command?

Reducing what I'm trying to do to a simple example:

env temp=`pwd` echo "$temp"

I get this message:

temp: Undefined variable.

How do I make it work (in a shell-agnostic way)? I'm expecting the result of
to be printed.

My actual requirement uses a complicated expression in place of
, a script in place of "
", and the variable
as an argument to that script.

Also, I want to set this variable only for this single command, and not for the whole shell (or any subsequent subshells).

Answer Source

How about something like this for sh and bash

(export temp=`pwd`; echo $temp)

And this for csh

csh -c 'setenv temp `pwd`; echo "$temp"'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download