Declaring Global Variable in Bash Scripting for Beginners

There might come a time when you’re working with bash scripting and you’re constantly setting variables with every new session you start. This can become tiresome and redundant after the third or fourth time doing it. But, did you know that you can set global variables for all users on a linux box with bash scripting so that you don’t have to set the same variables every time you start a new session?

The process to set global variables for all users is quite easy and I’ll dive into it here. But, there are a couple things to keep in mind when it comes to overwriting global variables - which I’ll mention towards the end.

A little bit about the export function

The built in bash function export is used to make variable and function declarations that will be passed on to child processes.

Let’s dive into that a bit.

Let’s say you have the file colors.sh. This is a file that contains shell script commands - which is why it has the .sh extension.

In [colors.sh](http://colors.sh) you have the following code:

echo "My favorite color is $COLOR."

This script will output (echo) the sentence and replace the variable $COLOR with whatever has been stored for that variable in that session.

If you were to run this file by typing ./colors.sh - the sentence would come out as:

My favorite color is .

This is because the variable definition has not been set for this process or the child processes it contains. [colors.sh](http://colors.sh) would be considered a child process.

One way to set variable definitions is through the shell with a command like COLOR="Blue".

If you run that command followed by ./colors.sh you’ll still get the output:

My favorite color is .

This has to do with the scope of your variable definition. These two separate processes, your shell script and the shell script file, aren’t communicating variable definitions between each other.

That brings us to the first way we can make these two communicate.

Temporary Variable Declaration

The first way we can make these two processes sync to the same variable definition is through the export function.

If we set the variable in our shell by running COLOR="Blue" followed by the command export COLOR, our [colors.sh](http://colors.sh) file will now be able to access that variable definition of COLOR.

Our output will now look like:

My favorite color is Blue.

Success! However, once we log out of this session or shut down the machine, that variable will not persist. This is only a temporary way to store the variable.

Export User Variables

A more permanent way is to edit the hidden .bashrc file. This file gets executed whenever the user logs in. That means any variables that are stored here in conjunction with the export function can be used in any other invocation of that variable.

You can edit the .bashrc file by running the command vi ~/.bashrc. Head down to the bottom and hit i to insert a new line then type export COLOR="Blue" followed by :wq.

Now that you’ve added that variable declaration to the .bashrc file, when you run ./colors.sh the output will have the color, right?

Unfortunately, no. The .bashrc only gets run when the user logs in. So, logout, log back in and run ./colors.sh. The output should be what you’re expecting now.

However, editing the .bashrc file only means that user can use those exported variables. So what if you want all users to be able to use the exported variables?

Export Global Variables

Similar to editing the .bashrc file, we’ll need to edit the /etc/profile file for all users to access exported variables.

We can do the exact same steps we did when exporting user variables by running the command vi /etc/profile. Head down to the bottom and hit i to insert a new line then type export COLOR="Blue" followed by :wq.

When you define a variable in /profile, it will overwrite variables with the same name for any users except root. Root’s user defined variables will always remain the same.

Conclusion and Further Resources

That’s how you can set variable definitions in different scopes. Whether you need to set variables as a user, globally, or just during a session, now you have the tools to do so.

I hope you found this guide useful and let me know if you have any questions in the comments below!

Here are some further resources too:

https://www.digitalocean.com/community/tutorials/export-command-linux https://www.digitalocean.com/community/tutorials/bashrc-file-in-linux https://www.ibm.com/docs/en/aix/7.2?topic=files-etcprofile-file