Question

You can set a variable for a single command like this:

MY_VARIABLE=my_value ./my_script.sh

You can hand off to another script like this:

exec ./my_script.sh

But when I try to do both like this:

exec MY_VARIABLE=my_value ./my_script.sh

I get an error:

exec: MY_VARIABLE=my_value: not found

Why is that?
Is there any way to do this?

Was it helpful?

Solution

You need to use env to specify the environment variable:

exec env MY_VARIABLE=my_value ./my_script.sh

If you want your script to start with an empty environment or with only the specified variables, use the -i option.

From man env:

   env - run a program in a modified environment

OTHER TIPS

In bash, you can set environment variables for a command by putting the assignments at the beginning of the command. This works the same for exec as any other command, so you write:

MYVARIABLE=my_value exec ./my_script.sh
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top