Bash Scripting for Beginners [Part 2] - Environment Variables

Bash Scripting for Beginners [Part 2] - Environment Variables

In the second part of the Bash Scripting tutorial we focus on Linux Environment Variables and what you can achieve with them.

On Linux, Environment Variables, serve multiple purposes, for one they contain information about your system and on the other hand they store user or system settings, for example the (shell) language or the editor to use with certain applications.

Printing the value of an Environment Variable is straigth forward, you can use echo for that.

$ echo $LANG
en_US.UTF-8

Defining an "local" Environment Variable is easy too, but has some caveats to look out for. Let's create a new one as sample:

$ MEAL="Pizza"
$ echo $MEAL
Pizza

You set the variable without the dollar sign. As we are using double quotes the value gets evaluated, as seen in the first part of the Bash Scripting tutorial. Make sure you don't use the dollar ($) sign, except you want to reference another variable.

$ MEAL="Pizza costs me a lot of \$" # Escaping
$ echo $MEAL
Pizza costs me a lot of $

Your Environment Variable names don't need to be upper-case, though by convention, this is the prefered way to differentiate them from (lower-case) variables defined in scripts. Use the underscore to split them into NICE_READABLE_VARIABLE_NAMES.

Local and Global Variables

Any variable can be modified, but as said above, there are some caveats to look out for. Let's take a look at the EDITOR variable, with it you can select the editor for certain applications, e.g. forcrontab -e or visudo (Yes, you can use it with nano too).

$ EDITOR="nano"
$ echo $EDITOR
nano
$ crontab -e # Not working

As you see this is not working, this is because we defined EDITOR as an "local" variable. crontab -e is a new process started by the shell, where the local variables are not "transferred" to.

In order to make the an local variable "global", we need to export it.

$ export EDITOR="nano"
$ echo $EDITOR
nano
$ crontab -e

Now you can edit your cron jobs in nano, instead of vi.

Changing an environment variable for just one command

Sometimes you don't want to change an Environment Variable for more than one command, this can be achieved by setting it, without export, inline just before the command.

$ EDITOR=vim crontab -e

Now crontab is opened with vim and after you exit the editor (:wq), the EDITOR variable is still at it's previous value. E.g. if you followed the steps before it should still be nano.

This can be quite useful for a multitude of things, for example looking at an manual in another language, without changing your shell language (LANG=de_DE.UTF-8 man man).

Persisting environment variables

When you define or update variables these changes are not stored over your shells lifespan. Once you close your Terminal they are lost.

To persist them they need to add them to your users shell startup file with export. For the bash you would usually go with the ~/.bashrc file.

Changing / Adding environment variables system wide is done in /etc/profile or the global config file of your shell in the /etc folder. Certain environment variables, for example $LANG (/etc/locale.conf) are stored in their own files though!

Important Environment Variables

LANG Language and Character set
PATH Folders in which the shell is looking for applications (e.g. /bin /usr/bin /sbin ..).
PS1 Defines what information the Bash prompt contains.
TZ Timezone
EDITOR Editor used for certain applications
HOME Home directory
USER Username running the shell
SHELL Shell being used (e.g. bash)
TERM Terminal being used (e.g. xterm, rxvt-unicode, alacritty)
Display X11 / Xorg display your terminal is running on (if any)
Extending the PATH variable

It's useful to include your users "bin" folder in the PATH variable, so your own scripts are available without the need to enter the full path.

$ mkdir ~/bin
$ export PATH="~/bin:$PATH"

With this export, the bin folder in your home directory is searched first, when the shell is looking for an application. Make sure your script is executable (chmod u+x scriptfile).

Commands around Environment Variables

export Without any parameters export shows local and global variables. `export` is also used to make local variables "global".
set Shows local, global variables and definitions (for example PS1) without parameters. You can also assign or define variables with it.
unset Deletes a variable.
env Lists all environment variables (like export without parameters)