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

Recently I wrote a script which sets an environment variable, take a look:

#!/bin/bash

echo "Pass a path:"
read path
echo $path

defaultPath=/home/$(whoami)/Desktop

if [ -n "$path" ]; then
    export my_var=$path
else
    echo "Path is empty! Exporting default path ..."
    export my_var=$defaultPath
fi

echo "Exported path: $my_var"

It works just great but the problem is that my_var is available just locally, I mean in console window where I ran the script.

How to write a script which allow me to export global environment variable which can be seen everywhere?


Solution 1:

Just run your shell script preceded by "." (dot space).

This causes the script to run the instructions in the original shell. Thus the variables still exist after the script finish

Ex:

cat setmyvar.sh
export myvar=exists

. ./setmyvar.sh

echo $myvar
exists

Solution 2:

Each and every shell has its own environment. There's no Universal environment that will magically appear in all console windows. An environment variable created in one shell cannot be accessed in another shell.

It's even more restrictive. If one shell spawns a subshell, that subshell has access to the parent's environment variables, but if that subshell creates an environment variable, it's not accessible in the parent shell.

If all of your shells need access to the same set of variables, you can create a startup file that will set them for you. This is done in BASH via the $HOME/.bash_profile file (or through $HOME/.profile if $HOME/.bash_profile doesn't exist) or through $HOME/.bashrc. Other shells have their own set of startup files. One is used for logins, and one is used for shells spawned without logins (and, as with bash, a third for non-interactive shells). See the manpage to learn exactly what startup scripts are used and what order they're executed).

You can try using shared memory, but I believe that only works while processes are running, so even if you figured out a way to set a piece of shared memory, it would go away as soon as that command is finished. (I've rarely used shared memory except for named pipes). Otherwise, there's really no way to set an environment variable in one shell and have another shell automatically pick it up. You can try using named pipes or writing that environment variable to a file for other shells to pick it up.

Imagine the problems that could happen if someone could change the environment of one shell without my knowledge.

Solution 3:

Actually I found an way to achieve this (which in my case was to use a bash script to set a number of security credentials)

I just call bash from inside the script and the spawned shell now has the export values

export API_USERNAME=abc
export API_PASSWORD=bbbb
bash

now calling the file using ~/.app-x-setup.sh will give me an interactive shell with those environment values setup

Solution 4:

You got to add the variable in your .profile located in /home/$USER/.profile

Yo can do that with this command:

echo 'TEST="hi"' >> $HOME/.profile

Or by edit the file with emacs, for example. If you want to set this variable for all users, you got to edit /etc/profile (root)

Solution 5:

There is no global environment, really, in UNIX.

Each process has an environment, originally inherited from the parent, but it is local to the process after the initial creation.

You can only modify your own, unless you go digging around in the process using a debugger.