Can I export a variable to the environment from a Bash script without sourcing it?
Is there any way to access to the
$VAR
by just executingexport.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 $FOOBAR
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/bashecho 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/bashexport FOO=BARexec "$@"$ ./set-vars3.sh printenv | grep FOOFOO=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).
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 $VARHELLO, 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.shHELLO, VARIABLE--.
The code: file test-export.sh
#!/bin/bash# Source env variablesource ./export.bash# echo out the variable in test scriptecho $VAR# unset the variableunset VAR# echo a few dotted linesecho "---"# now return VAR which is blankecho $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.shHELLO THEREHI THERE
File cat v1.sh
#!/bin/bashecho $VARecho $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.shHELLO THEREHI THEREecho $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/bashecho "VAR=HELLO THERE"
Now within script or your console run:
export "$(./export.bash)"
Try:
echo $VARHELLO THERE
Multiple values so long as you know what you are expecting in another script using the above method:
File export.bash
#!/bin/bashecho "VAR=HELLO THERE"echo "VAR1=HI THERE"
File test-export.sh
#!/bin/bashIFS=$'\n'for entries in $(./export.bash); do export $entriesdoneecho "round 1"echo $VARecho $VAR1for entries in $(./export.bash); do variable=$(echo $entries|awk -F"=" '{print $1}'); unset $variabledoneecho "round 2"echo $VARecho $VAR1
Now the results
./test-export.shround 1HELLO THEREHI THEREround 2.
And the final final update to auto assign, read the VARIABLES:
./test-export.shRound 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 THEREround 1 - we know what was exported and we will echo out known variablesHELLO THEREHI THERERound 2 - We will just return the variable names and unset themround 3 - Now we get nothing back
The script:
File test-export.sh
#!/bin/bashIFS=$'\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"doneecho "round 1 - we know what was exported and we will echo out known variables"echo $VARecho $VAR1echo "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 $variabledoneecho "round 3 - Now we get nothing back"echo $VARecho $VAR1
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