Global environment variables in a shell script
Asked Answered
F

6

104

How to set a global environment variable in a bash script?

If I do stuff like

#!/bin/bash
FOO=bar

...or

#!/bin/bash
export FOO=bar

...the vars seem to stay in the local context, whereas I'd like to keep using them after the script has finished executing.

Fere answered 23/9, 2009 at 6:25 Comment(0)
S
166

Run your script with .

. myscript.sh

This will run the script in the current shell environment.

export governs which variables will be available to new processes, so if you say

FOO=1
export BAR=2
./runScript.sh

then $BAR will be available in the environment of runScript.sh, but $FOO will not.

Spheroidicity answered 23/9, 2009 at 6:28 Comment(4)
Be careful with that first one. Without a slash, it will look in your path: use something like '. ./myscript.sh' if you want to ensure it runs a specific one.Cloak
source is an alias for .. So you could run source myscript.sh instead, if you wanted to be more explicit.Innkeeper
I am wondering what happens when i run a script with dot and a space. example . myscriptDanieladaniele
@Danieladaniele Ask a new question if you can't find it by reading the other answers on this page and/or googling, but this is a common FAQ; see also e.g. unix.stackexchange.com/questions/114300/…Hautemarne
C
63

When you run a shell script, it's done in a sub-shell so it cannot affect the parent shell's environment. You want to source the script by doing:

. ./setfoo.sh

This executes it in the context of the current shell, not as a sub shell.

From the bash man page:

. filename [arguments]
source filename [arguments]

Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.

If filename does not contain a slash, file names in PATH are used to find the directory containing filename.

The file searched for in PATH need not be executable. When bash is not in POSIX mode, the current directory is searched if no file is found in PATH.

If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.

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 status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.

Cloak answered 23/9, 2009 at 6:29 Comment(2)
Interesting - I've not seen that syntax before. Is this equivalent to source ./setfoo.sh?Presto
It's the same, just faster to type (and more compatible on older systems - ksh has no source command but it does have '.').Cloak
L
13

source myscript.sh is also feasible.

Description for linux command source:

source is a Unix command that evaluates the file following the command, 
as a list of commands, executed in the current context
Limestone answered 1/9, 2013 at 0:48 Comment(6)
Maybe just a tad more detail here. How about a very brief description of what source does?Transceiver
@PhillipCloud type man source in the terminal and you'll get what you want.Limestone
I'm familiar with source. Maybe the OP isn't.Transceiver
@PhillipCloud I would like some more detail, myself. If you want to edit the answer, you can definitely do that. Help the cause and all that... Thx!Rundown
man source may well return No manual entry for source. Try help source instead. This is because source is a command within the bash command language interpreter, so it has no man page of its own. Type help for all commands defined internally by the shell.Proxy
While Bash is the default shell on many Linux platforms, and the question asks about Bash, the question is relevant for other shells besides Bash. At the very least, this answer should mention that this solution is not portable to many other shells.Hautemarne
C
4
#!/bin/bash
export FOO=bar

or

#!/bin/bash
FOO=bar
export FOO

man export:

The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to word.

Crosspurpose answered 23/9, 2009 at 6:33 Comment(1)
Please, to every beginner in shell scripting: note there isn't any blank between the envvar name, '=' character and the value itself; this wouldn't work: export FOO = /mydir/barRudderpost
H
0

A common design is to have your script output a result, and require the cooperation of the caller. Then you can say, for example,

eval "$(yourscript)"

or perhaps less dangerously

cd "$(yourscript)"

This extends to tools in other languages besides shell script.

Hautemarne answered 7/11, 2022 at 6:52 Comment(0)
S
0

In your shell script, write the variables to another file like below and source these files in your ~/.bashrc or ~/.zshrc

echo "export FOO=bar" >> environment.sh

In your ~/.bashrc or ~/.zshrc, source it like below:

source Path-to-file/environment.sh

You can then access it globally.

Stephanstephana answered 9/11, 2022 at 7:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.