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 get no value !
echo $VAR
Is there any way to access to the $VAR
by just executing export.bash without sourcing it ?
Found an interesting and neat way to export environment variables from a file:
in
env.vars
:test script:
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:Another way is to have the script, rather than setting an environment variable, print commands that will set the environment variable:
Note that the
$(...)
will join the output into a single line. If you have more than one thing to set, you should add semicolons to the printed commands so they're still valid after being joined together.A third approach is to have a script that sets your environment variable(s) internally and then invokes a specified command with that environment:
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).
The answer is no, but for me I did the following
the script: myExport
an alias in my .bashrc
Still you source it, but maybe in this way it is more useable and it is interesting for someone else.
Execute
Any variables you source from a file after this will be exported in your shell.
When you're done execute. This will disable allexport mode.
In order to export out the VAR variable first the most logical and seems working way is to source the variable:
or
Now when echoing from main shell it works
We will now reset VAR
Now we will execute a script to source the variable then unset it :
the code: cat test-export.sh
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 has multiple echo values)
cat v1.sh
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:
add this to your .bashrc
source your bashrc file and you can do like above any time ...
Anyhow back to the rest of it..
This has made it available globally then executed the script..
simply echo it out then run export on the echo !
cat export.bash
Now within script or your console run:
Try:
Multiple values so long as you know what you are expecting in another script using above method:
cat export.bash
cat test-export.sh
Now the results
and the final final update to auto assign read the VARIABLES:
The script: cat test-export.sh
Maybe you can write a function in ~/.zshrc, ~/.bashrc .
Beacause of use variable outside, you can avoid write script file.