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
Defining an "local" Environment Variable is easy too, but has some caveats to look out for. Let's create a new one as sample:
$ echo $MEAL
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.
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. for
crontab -e or
visudo (Yes, you can use it with nano too).
$ echo $EDITOR
$ 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
$ crontab -e
Now you can edit your cron jobs in nano, instead of vi.
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).
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
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!
|Language and Character set
|Folders in which the shell is looking for applications (e.g. /bin /usr/bin /sbin ..).
|Defines what information the Bash prompt contains.
|Editor used for certain applications
|Username running the shell
|Shell being used (e.g. bash)
|Terminal being used (e.g. xterm, rxvt-unicode, alacritty)
|X11 / Xorg display your terminal is running on (if any)
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).
|Without any parameters export shows local and global variables. `export` is also used to make local variables "global".
|Shows local, global variables and definitions (for example PS1) without parameters. You can also assign or define variables with it.
|Deletes a variable.
|Lists all environment variables (like export without parameters)