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 $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).
How do you export a variable through shell script?
You can't do an export through a shell script, because a shell script runs in a child shell process, and only children of the child shell would inherit the export.
The reason for using source is to have the current shell execute the commands
It's very common to place export commands in a file such as .bashrc which a bash will source on startup (or similar files for other shells)
Another idea is that you could create a shell script which generates an export command as it's output:
shell$ cat > script.sh
#!/bin/sh
echo export foo=bar
^D
chmod u+x script.sh
And then have the current shell execute that output
shell$ `./script.sh`
shell$ echo $foo
bar
shell$ /bin/sh
$ echo $foo
bar
(note above that the invocation of the script is surrounded by backticks, to cause the shell to execute the output of the script)
Trying to export a bash variable from an .sh file to another file that runs it
Variables are exported from a parent to a child, not vice versa. script_2.sh
is called in a different shell whose environment doesn't propagate back to the parent shell.
Source the script (using the .
) to call it in the same shell. You then don't even need to export the value.
. ./scripts/script_2.sh
Unable to export the variable through script file
Running your script like
. ./script
or
source script
would execute your script in the current shell context (without creating a subshell) and the environment variables set within the script would be available in your current shell.
From the manual:
. filename [arguments]
Read and execute commands from the filename argument in the current
shell context. If filename does not contain a slash, thePATH
variable
is used to find filename. When Bash is not inPOSIX
mode, the current
directory is searched if filename is not found in$PATH
. If any
arguments are supplied, they become the positional parameters when
filename is executed. Otherwise the positional parameters are
unchanged. The return status is the exit status of the last command
executed, or zero if no commands are executed. If filename is not
found, or cannot be read, the return status is non-zero. This builtin
is equivalent tosource
.
Exporting variable from one shell script to another script in bash
use source
:
source ./set-vars1.sh
Or:
. ./set-vars1.sh
#The first . is intentional
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 do I export environment variables using a script in the same shell in python?
One way to do it is to run export.py
in the same process, as user1934428 suggested:
import os
import sys
os.environ['ENV'] = 'Demo'
sys.path.append('/home/rishabh/')
import export # runs export.py in the same process
print(os.environ.get('RDS_DB_NAME'))
This assumes there are no __name__ == '__main__'
checks inside export.py
.
You only need the sys.path
line if export.py
is in a different directory than your current script.
Related Topics
Memory Limit to a 32-Bit Process Running on a 64-Bit Linux Os
How to Set Rpath and Runpath with Gcc/Ld
How to Automatically Add User Account and Password with a Bash Script
Run Script with Rc.Local: Script Works, But Not at Boot
Why No Output Is Shown When Using Grep Twice
What Are the Rules for Valid Identifiers (E.G. Functions, Vars, etc) in Bash
How to Count Number of Files in Each Directory
How to Run a Cron Job Inside a Docker Container
How to Install Nuget from Command Line on Linux
Get a Browser Rendered HTML+Javascript
Signing Windows Application on Linux-Based Distros
How to Get Position of Cursor in Terminal
Docker Command Can't Connect to Docker Daemon
Switching Users Inside Docker Image to a Non-Root User
Specifying Udp Receive Buffer Size at Runtime in Linux
Docker: Are Docker Links Deprecated
How to Have the Cp Command Create Any Necessary Folders for Copying a File to a Destination