-->

Learning about Variable Arrays On Linux

A really cool feature of environment variables is that they can be used as arrays. An array is a variable that can hold multiple values. Values can be referenced either individually or as a whole for the entire array.

Learning about Variable Arrays On Linux
Learning about Variable Arrays On Linux


To set multiple values for an environment variable, just list them in parentheses, with values separated by spaces:

$ mytest=(one two three four five)
$

Not much excitement there. If you try to display the array as a normal environment
variable, you’ll be disappointed:

$ echo $mytest
one
$

Only the first value in the array appears. To reference an individual array element, you must use a numerical index value, which represents its place in the array. The numeric value is enclosed in square brackets:

$ echo ${mytest[2]}
three
$

Tip
Environment variable arrays start with an index value of zero. This can be confusing.

To display an entire array variable, you use the asterisk wildcard character as the index value:

$ echo ${mytest[*]}
one two three four five
$

You can also change the value of an individual index position:

$ mytest[2]=seven
$$
echo ${mytest[*]}
one two seven four five
$

You can even use the unset command to remove an individual value within the array, but be careful, because this gets tricky. Watch this example:

$ unset mytest[2]
$$
echo ${mytest[*]}
one two four five
$$
echo ${mytest[2]}
$ echo ${mytest[3]}
four
$

This example uses the unset command to remove the value at index value 2. When you display the array, it appears that the other index values just dropped down one. However, if you specifically display the data at index value 2, you see that that location is empty.

Finally, you can remove the entire array just by using the array name in the unset command:

$ unset mytest
$$
echo ${mytest[*]}
$

Sometimes variable arrays just complicate matters, so they’re often not used in shell script programming. They’re not very portable to other shell environments, which is a downside if you do lots of shell programming for different shells. Some bash system environment variables use arrays (such as BASH_VERSINFO), but overall you probably won’t run into them very often.

Summary

This article examined the world of Linux environment variables. Global environment variables can be accessed from any child shell spawned by the parent shell in which they’re defined. Local environment variables can be accessed only from the process in which they’re defined.

The Linux system uses both global and local environment variables to store information about the system environment. You can access this information from the shell command line interface, as well as within shell scripts. The bash shell uses the system environment variables defined in the original Unix Bourne shell, as well as lots of new environment variables. The PATH environment variable defines the search pattern the bash shell takes to find an executable command. You can modify the PATH environment variable to add your own directories, or even the current directory symbol, to make running your programs easier.

You can also create your own global and local environment variables for your own use. After you create an environment variable, it’s accessible for the entire duration of your shell session.

The bash shell executes several startup files when it starts up. These startup files can contain environment variable definitions to set standard environment variables for each bash session. When you log in to the Linux system, the bash shell accesses the /etc/profile startup file and three local startup files for each user, $HOME/.bash_profile, $HOME/.bash_login, and $HOME/.profile. Users can customize these files to include environment variables and startup scripts for their own use.

Finally, this chapter discussed the use of environment variable arrays. These environment variables can contain multiple values in a single variable. You can access the values either individually by referencing an index value or as a whole by referencing the entire environment variable array name.

The next chapter dives into the world of Linux file permissions. This is possibly the most difficult topic for novice Linux users. However, to write good shell scripts, you need to understand how file permissions work and be able to use them on your Linux system.

0 Response to "Learning about Variable Arrays On Linux"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel