0

If the command my_command has this output:

VAR1=some_value
VAR2=other_value

is it possible to call my_command from a shell script so that both VAR1 and VAR2 are saved as environment variables?

I tried the following ways, and none of them works:

#!/bin/bash
source my_command
export `my_command`

However if I manually enter

export `my_command`

in a terminal console, then both variables are saved.

marked as duplicate by muru, jayhendren, G-Man, mdpc, Archemar Jan 4 '18 at 5:42

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @ilkkachu If export ... works in a shell and not in a script, is it not the same as that one? – muru Jan 3 '18 at 18:46
  • @ilkkachu it is if the script contains export `mycommand` like in the question. – muru Jan 3 '18 at 18:50
  • @muru, well, right, that's true. – ilkkachu Jan 3 '18 at 18:55
3

If you have my_command like this:

#!/bin/sh
echo 'VAR1=some_value'
echo 'VAR2=other_value'

running it obviously prints

VAR1=some_value
VAR2=other_value

Now, source my_command will run the script, in the current shell, which will just produce the assignments as output. You would use source if my_command didn't print the assignments, but executed then directly.

export $(my_command) will take the output of the script, and pass it to export, which will put the variables in the current shell environment, but it only works as long as the variables in question don't contain whitespace or glob characters, since the command substitution is unquoted. Putting "$(my_command)" in quotes doesn't really help, because then export sees VAR1=some_value\nVAR2=other_value as one argument, and only VAR1 is set.

Note that having running export $(my_command) or setting variables in any other way within another script will not set those variables in the parent shell. If you want the assignments in your interactive shell, you must run them there, but you could wrap the assigments in a function or source a script that makes the assignments.


One, obvious solution would be to do what ssh-agent does, and have the script output export commands for the variables it sets. Then it would be enough to run eval "$(my_command)".

If we don't want to do that, we can use set -a before the script to have the shell automatically export any variables that are set.

So,

$ set -a
$ eval "$(my_command)"

In Bash, we can use declare -p to check that the variables are exported (-x):

$ declare -p VAR1 VAR2
declare -x VAR1="some_value"
declare -x VAR2="other_value"

Using eval here will allow the script to output quoted strings like VAR1="foo bar", and they will be handled correctly. eval will also run anything else the script my_command outputs, not just assignments, but if you didn't trust my_command, you shouldn't run it at all.

0

You can't set variables of the parent shell in a shell script you call from it. Either

  • set the variables as usual in the script but source it: . my_command
  • have the script print the variables to be set to stdout and evaluate the result

    $ cat foo.sh 
    #!/bin/sh
    echo FOO=foo
    echo BAR=bar
    $ ./foo.sh 
    FOO=foo
    BAR=bar
    $ eval $(./foo.sh)
    $ echo $FOO
    foo
    $ echo $BAR
    bar
    

Not the answer you're looking for? Browse other questions tagged or ask your own question.