Setting Environment Variables - Shell Scripting

You can set your own environment variables directly from the bash shell.This section shows how to create your own environment variables and reference them from your interactive shell or shell script program.

Setting local environment variables

Once you start a bash shell (or spawn a shell script), you’re allowed to create local variables that are visible within your shell process. You can assign either a numeric or a string value to an environment variable by assigning the variable to a value using the equal sign:

$ test=testing
$ echo $test
testing
$

That was simple! Now any time you need to reference the value of the test environment variable, just reference it by the name $test.

If you need to assign a string value that contains spaces, you’ll need to use a single quotation
mark to delineate the beginning and the end of the string:

$ test=testing a long string
-bash: a: command not found
$ test=’testing a long string’
$ echo $test
testing a long string
$

Without the single quotation marks, the bash shell assumes that the next character is another command to process. Notice that for the local environment variable I defined, I used lower-case letters, while the system environment variables we’ve seen so far have all used upper-case letters.

This is a standard convention in the bash shell. If you create new environment variables, it is recommended (but not required) that you use lower-case letters. This helps distinguish your personal environment variables from the scores of system environment variables.
Once you set a local environment variable, it’s available for use anywhere within your shell process.

However, if you spawn another shell, it’s not available in the child shell:

$ bash
$ echo $test
$ exit
exit
$ echo $test
testing a long string
$

In this example I started a child shell. As you can see, the test environment variable is not available in the child shell (it contains a blank value). After I exited the child shell and returned to the original shell, the local environment variable was still available.

Similarly, if you set a local environment variable in a child process, once you leave the child process the local environment variable is no longer available:

$ bash
$ test=testing
$ echo $test
testing
$ exit
exit
$ echo $test
$

The test environment variable set in the child shell doesn’t exist when I go back to the parent shell.

Setting global environment variables
Global environment variables are visible from any child processes created by the process that sets the global environment variable. The method used to create a global environment variable is to create a local environment variable, then export it to the global environment.

This is done by using the export command:

$ echo $test
testing a long string
$ export test
$ bash
$ echo $test
testing a long string
$

After exporting the local environment variable test, I started a new shell process and viewed the value of the test environment variable. This time, the environment variable kept its value, as the export command made it global.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Shell Scripting Topics