99

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.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Alex
  • 43,191
  • 44
  • 96
  • 127

7 Answers7

161

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.

mob
  • 117,087
  • 18
  • 149
  • 283
  • 30
    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. – paxdiablo Sep 23 '09 at 06:47
  • 15
    `source` is an alias for `.`. So you could run `source myscript.sh` instead, if you wanted to be more explicit. – Ehtesh Choudhury Jul 01 '13 at 17:14
  • I am wondering what happens when i run a script with dot and a space. example . myscript – cNgamba Sep 09 '19 at 20:23
  • @cNgamba 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. https://unix.stackexchange.com/questions/114300/whats-the-meaning-of-a-dot-before-a-command-in-shell – tripleee Mar 01 '21 at 19:16
60

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.

Alexis Tyler
  • 1,394
  • 6
  • 30
  • 48
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
12

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
CDT
  • 10,165
  • 18
  • 66
  • 97
  • Maybe just a tad more detail here. How about a very brief description of what `source` does? – Phillip Cloud Sep 01 '13 at 01:08
  • @PhillipCloud type `man source` in the terminal and you'll get what you want. – CDT Sep 07 '13 at 01:19
  • 5
    I'm familiar with `source`. Maybe the OP isn't. – Phillip Cloud Sep 07 '13 at 01:25
  • 2
    @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! – Dan Rosenstark Jul 23 '14 at 21:46
  • 1
    `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. – XavierStuvw Jan 09 '22 at 16:02
  • 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. – tripleee Aug 17 '23 at 06:41
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.

Andrejs Cainikovs
  • 27,428
  • 2
  • 75
  • 95
  • 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/bar` – joninx May 31 '17 at 08:45
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.

tripleee
  • 175,061
  • 34
  • 275
  • 318
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.

Druid
  • 6,423
  • 4
  • 41
  • 56
Venkat
  • 1
  • 1
-3
FOO=bar
export FOO
Niko
  • 6,133
  • 2
  • 37
  • 49