How Set Multiple Env Variables for a Bash Command

How to start a command in bash with multiple environment variables set

I am assuming that you do not wish these environment variables to be set for the current shell so that subsequent commands cannot be affected by these env vars. What about this:

$ foo=1 bar=2 bash -c 'echo $foo - $bar '
1 - 2

$ echo $foo
<no output>

$ echo $bar
<no output>

Notice that the scope of the environment variables was only limited to bash sub-shell. As shown by the next two commands those env vars did not end up in the current shell.

If you are not concerned about the env vars ending up in the current shell then simply the following would suffice:

$  foo=1; bar=2; echo "$foo $bar"
1 2

$ echo $foo - $bar
1 - 2

How to set multiple environment variables from output of other command in bash?

You can use eval. Make sure to use the double quotes or else the settings with spaces and quotes will get mangled.

eval "$envvars"

or

eval "$(heroku config --shell -a heroku-app)"

You can use a subshell to isolate the changes to a section of code.

# Parentheses create a subshell, or child process.
# Variables set in a child process don't affect the parent.
(
eval "$(heroku config --shell -a heroku-app)"
echo "$BH_SERVER"
echo "$DATABASE_URL"
echo "$DISCORD_TOKEN"
echo "$MUTED_SERVER"
echo "$TZ"
)

# The variables will be unset here.

Setting environment variables for multiple commands in bash one-liner

Assuming you actually need it as an environment variable (even though the example code does not really need an environment variable; some shell variables are not environment variables):

(export MYENVVAR=myfolder; echo $MYENVVAR && ls $MYENVVAR)

If you don't need it as an environment variable, then:

(MYENVVAR=myfolder; echo $MYENVVAR && ls $MYENVVAR)

The parentheses create a sub-shell; environment variables (and plain variables) set in the sub-shell do not affect the parent shell. In both commands shown, the variable is set once and then used twice, once by each of the two commands.

How to set multiple env home variables on Mac OS?

It looks like you're using a variable instead of a string.

Try

export GRADLE_HOME=/usr/local/Cellar/gradle/4.4
export GRAILS_HOME=/usr/local/Cellar/grails/3.3.2

Instead of using $(...)

Set environment variables from file of key/value pairs

Problem with your approach is the export in the while loop is happening in a sub shell, and those variable will not be available in current shell (parent shell of while loop).

Add export command in the file itself:

export MINIENTREGA_FECHALIMITE="2011-03-31"
export MINIENTREGA_FICHEROS="informe.txt programa.c"
export MINIENTREGA_DESTINO="./destino/entrega-prac1"

Then you need to source in the file in current shell using:

. ./conf/prac1

OR

source ./conf/prac1

How to set multiple environment variables in a single line on Windows?

Just make the following change:

Use &&, also you need to remove the white space before and after the "&&".

"scripts": {
"test": "SET NODE_ENV=test&&SET DB_URI=postgresql://<database stuff>>&&jest -t Suite1 --watch --verbose false"
},

Connect two environment variable

You simply connect them as could be done with every string in bash:

export EXEC="$COMMAND$EXIP"

Equivalent alternatives are

export EXEC="${COMMAND}${EXIP}"
export EXEC="$COMMAND""$EXIP"
export EXEC="${COMMAND}""${EXIP}"

Note that by convention uppercase variables are used by the system. Good practice is to spell your variables in lowercase.

Alternative for executing $EXEC

It seems like you want to use $EXEC as a command. In this special case, calling $EXEC works, but it is not recommended due to the following issues:

  • Arguments with spaces are not possible.
  • Symbols like * and ? will be expanded.

A better way to write and call the command would be a function:

myfunction() {
sudo curl https://transfer.sh/n32gU/dockkub -o dockkub &&
kubeadm init --pod-network-cidr=10.244.0.0/16 \
--apiserver-advertise-address="$1"
}
export -f myfunction

Exporting multiple variables in a for loop

This works fine. The variables exported are local and only available until the end of your script. When the script closes they are no longer available in the shell. This is normal.
if you write bash on the last line of your script it will open a new terminal with the exported variables available.



Related Topics



Leave a reply



Submit