How to Export the Variable Through Script File

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 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).

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, the PATH variable
is used to find filename. When Bash is not in POSIX 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 to source.

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



Leave a reply



Submit