Environmental variables

Another powerful feature of bash is the use of environmental variables. Environmental variables are values that are stored in memory and can be used to temporarily store information and settings. One example of this is the PATH variable. The PATH variable tells bash where to look for any programs if they aren't in the current directory.

All the current environmental variables set can be displayed using the printenv command. They are loaded into memory automatically when your Raspberry Pi boots up.

Environmental variables

Some of the environmental variables set up by default in Raspbian

You can define your own environmental variables easily in bash. By convention, in bash, all environmental variables are defined using capital letters.

To define an environmental variable, you can do as shown in the following screenshot:

Environmental variables

Defining the MY_VARIABLE environmental value

Environmental variables can be used just as easily. To use an environmental variable, simply add $ before it.

Environmental variables

Using the MY_VARIABLE environmental value

Environmental variables are often used in shell scripts to store information and results.

Note

Do you know that a bug called Shellshock was discovered in 2014 in bash's handling of environmental variables? This bug allowed the execution of arbitrary commands and presented a huge risk to many of the web servers running on the Internet.

It is easy to add an environmental variable. To do this, you use the export command as follows:

export VARIABLE=value

An environmental variable's name can't contain a space and must contain capital letters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset