-->

Setting Local and Global User-Defined Variables On Linux

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

Setting Local and Global User-Defined Variables On Linux
Setting Local and Global User-Defined Variables On Linux

Setting local user-defined variables

After you start a bash shell (or spawn a shell script), you’re allowed to create local userdefined 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:

$ echo $my_variable
$ my_variable=Hello
$$
echo $my_variable
Hello

That was simple! Now, any time you need to reference the my_variable user-defined variable’s value, just reference it by the name $my_variable.

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

$ my_variable=Hello World
-bash: World: command not found
$$
my_variable=“Hello World”
$$
echo $my_variable
Hello World
$

Without the quotation marks, the bash shell assumes that the next word is another command to process. Notice that for the local variable you defined, you used lowercase letters, while the system environment variables you’ve seen so far have all used uppercase letters.

TipThe standard bash shell convention is for all environment variables to use uppercase letters. If you are creating a local variable for yourself and your own shell scripts, use lowercase letters. Variables are case sensitive. By keeping your user-defined local variables lowercase, you avoid the potential disaster of redefining a system environment variable.

It’s extremely important that you not use spaces between the variable name, the equal sign, and the value. If you put any spaces in the assignment, the bash shell interprets the value as a separate command:

$ my_variable = “Hello World”
-bash: my_variable: command not found
$

After you set a local 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:

$ my_variable=“Hello World”
$$
bash
$$
echo $my_variable
$ exit
exit
$$
echo $my_variable
Hello World
$

In this example, a child shell was spawned. The user-defined my_variable was not available in the child shell. This is demonstrated by the blank line returned after the echo $my_variable command. After the child shell was exited and returned to the original shell, the local variable was still available.
Similarly, if you set a local variable in a child process, after you leave the child process, the local variable is no longer available:

$ echo $my_child_variable
$ bash
$$
my_child_variable=“Hello Little World”
$$
echo $my_child_variable
Hello Little World
$$
exit
exit
$$
echo $my_child_variable
$

The local variable set within the child shell doesn’t exist after a return to the parent shell. You can change this behavior by turning your local user-defined variable into a global environment variable.

Setting global environment variables

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

This is done by using the export command and the variable name minus the dollar sign:

$ my_variable=“I am Global now”
$$
export my_variable
$$
echo $my_variable
I am Global now
$$
bash
$$
echo $my_variable
I am Global now
$$
exit
exit
$$
echo $my_variable
I am Global now
$

After defining and exporting the local variable my_variable, a child shell was started by the bash command. The child shell was able to properly display the my_variable variable’s value. The variable kept its value, because the export command made it a global environment variable.

Changing a global environment variable within a child shell does not affect the variable’s value in the parent shell:

$ my_variable=“I am Global now”
$ export my_variable
$$
echo $my_variable
I am Global now
$$
bash
$$
echo $my_variable
I am Global now
$$
my_variable=“Null”
$$
echo $my_variable
Null
$$
exit
exit
$$
echo $my_variable
I am Global now
$

After defining and exporting the variable my_variable, a subshell was started by the bash command.

The subshell properly displayed the value of the my_variable global environment variable. The variable’s value was then changed by the child shell. However, the variable’s value was modified only within the child shell and not in the parent’s shell.

A child shell cannot even use the export command to change the parent shell’s global environment variable’s value:

$ my_variable=“I am Global now”
$ export my_variable
$$
echo $my_variable
I am Global now
$$
bash
$$
echo $my_variable
I am Global now
$$
my_variable=“Null”
$$
export my_variable
$$
echo $my_variable
Null
$$
exit
exit
$$
echo $my_variable
I am Global now
$

Even though the child shell redefined and exported the variable my_variable, the parent shell’s my_variable variable kept its original value.

0 Response to "Setting Local and Global User-Defined Variables On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel