363
votes

Suppose that I have this script:

export.bash:

#! /usr/bin/env bash
export VAR="HELLO, VARIABLE"

When I execute the script and try to access to the $VAR, I don't get any value!

echo $VAR

Is there a way to access the $VAR by just executing export.bash without sourcing it?

8
You could try using an alias instead of a script to define your variable.jdigital
The script that I'm working on, is some kind of a java virtual manager , I do some computation then I should export a $JAVA_HOME to the env, and append it to PATH.Salah Eddine Taouririt
Well, you could write the info to a file and then read it back in, but sourcing seems much easier.jdigital
child processes cannot alter their parent's environment. The parent has to choose to alter itself (either with source or eval or ...)glenn jackman
To read more on source, man source won't work because source is built into bash, you need to run help sourcepeterchaula

8 Answers

395
votes

Is there any way to access to the $VAR by just executing export.bash without sourcing it ?

Quick answer: No.

But there are several possible workarounds.

The most obvious one, which you've already mentioned, is to use source or . to execute the script in the context of the calling shell:

$ cat set-vars1.sh 
export FOO=BAR
$ . set-vars1.sh 
$ echo $FOO
BAR

Another way is to have the script, rather than setting an environment variable, print commands that will set the environment variable:

$ cat set-vars2.sh
#!/bin/bash
echo export FOO=BAR
$ eval "$(./set-vars2.sh)"
$ echo "$FOO"
BAR

A third approach is to have a script that sets your environment variable(s) internally and then invokes a specified command with that environment:

$ cat set-vars3.sh
#!/bin/bash
export FOO=BAR
exec "$@"
$ ./set-vars3.sh printenv | grep FOO
FOO=BAR

This last approach can be quite useful, though it's inconvenient for interactive use since it doesn't give you the settings in your current shell (with all the other settings and history you've built up).

70
votes

In order to export out the VAR variable first, the most logical and seemly working way is to source the variable:

. ./export.bash

or

source ./export.bash

Now when echoing from the main shell, it works:

echo $VAR
HELLO, VARIABLE

We will now reset VAR:

export VAR=""
echo $VAR

Now we will execute a script to source the variable then unset it:

./test-export.sh
HELLO, VARIABLE
--
.

The code: file test-export.sh

#!/bin/bash
# Source env variable
source ./export.bash

# echo out the variable in test script
echo $VAR

# unset the variable
unset VAR
# echo a few dotted lines
echo "---"
# now return VAR which is blank
echo $VAR

Here is one way:

Please note: The exports are limited to the script that execute the exports in your main console - so as far as a cron job I would add it like the console like below... for the command part still questionable: here is how you would run in from your shell:

On your command prompt (so long as the export.bash file has multiple echo values)

IFS=$'\n'; for entries in $(./export.bash); do  export $entries;  done; ./v1.sh
HELLO THERE
HI THERE

File cat v1.sh

#!/bin/bash
echo $VAR
echo $VAR1

Now so long as this is for your usage - you could make the variables available for your scripts at any time by doing a Bash alias like this:

myvars ./v1.sh
HELLO THERE
HI THERE

echo $VAR

.

Add this to your .bashrc file:

function myvars() {
    IFS=$'\n';
    for entries in $(./export.bash); do  export $entries;  done;

    "$@";

    for entries in $(./export.bash); do variable=$(echo $entries|awk -F"=" '{print $1}'); unset $variable;
    done
}

Source your .bashrc file and you can do like the above any time...

Anyhow back to the rest of it...

This has made it available globally then executed the script...

Simply echo it out and run export on the echo!

File export.bash

#!/bin/bash
echo "VAR=HELLO THERE"

Now within script or your console run:

export "$(./export.bash)"

Try:

echo $VAR
HELLO THERE

Multiple values so long as you know what you are expecting in another script using the above method:

File export.bash

#!/bin/bash
echo "VAR=HELLO THERE"
echo "VAR1=HI THERE"

File test-export.sh

#!/bin/bash

IFS=$'\n'
for entries in $(./export.bash); do
    export $entries
done

echo "round 1"
echo $VAR
echo $VAR1

for entries in $(./export.bash); do
    variable=$(echo $entries|awk -F"=" '{print $1}');
    unset $variable
done

echo "round 2"
echo $VAR
echo $VAR1

Now the results

./test-export.sh
round 1
HELLO THERE
HI THERE
round 2


.

And the final final update to auto assign, read the VARIABLES:

./test-export.sh
Round 0 - Export out then find variable name -
Set current variable to the variable exported then echo its value
$VAR has value of HELLO THERE
$VAR1 has value of HI THERE
round 1 - we know what was exported and we will echo out known variables
HELLO THERE
HI THERE
Round 2 - We will just return the variable names and unset them
round 3 - Now we get nothing back

The script:

File test-export.sh

#!/bin/bash

IFS=$'\n'
echo "Round 0 - Export out then find variable name - "
echo "Set current variable to the variable exported then echo its value"
for entries in $(./export.bash); do
    variable=$(echo $entries|awk -F"=" '{print $1}');
    export $entries
    eval current_variable=\$$variable
    echo "\$$variable has value of $current_variable"
done


echo "round 1 - we know what was exported and we will echo out known variables"
echo $VAR
echo $VAR1

echo "Round 2 - We will just return the variable names and unset them "
for entries in $(./export.bash); do
    variable=$(echo $entries|awk -F"=" '{print $1}');
    unset $variable
done

echo "round 3 - Now we get nothing back"
echo $VAR
echo $VAR1
37
votes

Execute

set -o allexport

Any variables you source from a file after this will be exported in your shell.

source conf-file

When you're done execute. This will disable allexport mode.

set +o allexport
19
votes

I found an interesting and neat way to export environment variables from a file:

In file env.vars:

foo=test

Test script:

eval `cat env.vars`
echo $foo         # => test
sh -c 'echo $foo' # =>

export eval `cat env.vars`
echo $foo         # => test
sh -c 'echo $foo' # => test

# a better one. "--" stops processing options,
# key=value list given as parameters
export -- `cat env.vars`
echo $foo         # => test
sh -c 'echo $foo' # => test
8
votes

Another workaround that, depends on the case, it could be useful: creating another bash script that inherits the exported variable. It is a particular case of Keith Thompson's answer, will all of those drawbacks.

File export.bash:

# !/bin/bash
export VAR="HELLO, VARIABLE"
bash

Now:

./export.bash
echo $VAR
2
votes

The answer is no, but for me I did the following

The script:

myExport

#! \bin\bash
export $1

An alias in my .bashrc file:

alias myExport='source myExport'

Still you source it, but maybe in this way it is more useable and it is interesting for someone else.

2
votes

Maybe you can add a function in ~/.zshrc or ~/.bashrc.

# set my env
[ -s ~/.env ] && export MYENV=`cat ~/.env`
function myenv() { [[ -s ~/.env ]] && echo $argv > ~/.env && export MYENV=$argv }

Because of the use of a variable outside, you can avoid the use of a script file.

0
votes

I don't think this can be done, but I found a workaround using alias. It will only work when you place your script in your scripts directory. Otherwise your alias will have an invalid name.

The only point to the workaround is to be able to have a function inside a file with the same name and not have to bother sourcing it before using it. Add the following code to file ~/.bashrc:

alias myFunction='unalias myFunction && . myFunction && myFunction "$@"'

You can now call myFunction without sourcing it first.