Setting variables with executing a command with exec Setting variables with executing a command with exec bash bash

Setting variables with executing a command with exec


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


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