I need to set an environment variable for a command that is being called via nice. [Bash and dash, Slackware and FreeBSD.]
$ JIM=20 nice -n 10 echo $JIM
$
Nope, echo
can't see JIM
$ nice -n 10 JIM=20 echo $JIM
nice: JIM=20: No such file or directory
$
nice
don't like it.
I can export JIM and then echo can see it, but now I'm polluting the environment. Unclean!
I tried the "–" option to signify end of command line variables for nice
, but it makes no difference, nice
complains. I can't believe no one has wanted to do this before, so I'm probably making a very basic error.
Any ideas?
Best Answer
does pass the
JIM=20
environment variable tonice
, but it's notnice
norecho
that expands$JIM
, that's the shell.The shell forks a process and executes:
nice
sets the niceness and then executes in the same process:So
echo
does receiveJIM=20
in its environment, butecho
doesn't do anything with its environment.Had you run:
Then
sh
would have done something with that environment variable. Shells map the environment variables they receive to shell variables. So above, thatsh
would have set its$JIM
variable to20
and called itsecho
builtin with20
as argument.