'

Linux Environment Variables primer.

November 17th, 2016 by Andrea Matesi 225 Views

 

 

What Linux Environment Variables are.

Linux environment variables are "containers of data of some sort".

They're especially useful when playing with scripts.

By default, your Linux system declares many environment variables at boot.

To find out your default environment variables, type "export" on a terminal:

Standard Linux Environment Variables.

  • Export command output example.

The "export" command will provide you a list of all your default (System+User-declared) environment variables (similarly to the ones shown on the above screenshot).

If you need to set an environment variable, you'll have two (2) ways to go about it (either/or):

  • Declare the variable inside a file or a script.

OR

  • Declare variables on your bash prompt.

 

 

How to declare environment variables.

When you declare an environment variable (var from now on) on bash, you'll have to use the "export" command before the var name, as follows:

export VARNAME="stuff"

 

When you declare a var inside a script, you'll have to declare it as such:

(..)

VARNAME="stuff"

(..)

Please Note - the export command is not needed while scripting.

 

How to use environment variables.

The previous 2 commands, basically, do the same thing: they declare a container for the "stuff"-string.

Please note that ONLY when you're using your vars (ie. either inside your script(s) or from a very long command), you'll have to refer to the var with a $-symbol in front of the var name.

export-varname-stuff-example

"echo $VARNAME" command example.

 

Save your environment variables before rebooting!

If you wish to reinitialise your vars (and content) between reboots, there are two (2) other ways to do it: either declare your vars as:

  1. System-wide vars.
  2. User vars.

System-wide vars are loaded as soon as the system boots up (ie. no user interaction required - they belong to the system without having you to login first).

On the contrary, User vars are loaded only after a user logs in (ie. as part of your login process).

  • System-wide vars are usually declared inside /etc/profile.
  • User vars are usually declared within /home/jack/.profile.

So, depending on your vars content, you'll have to decide first how you'd like your vars to get loaded.

Namely, to declare a var inside a file, simply open one up one of the two files (as per above bullet points) with your fav editor, enter the var name and its content (as described at the beginning of my post - just remember to omit the $ symbol), save and off you go.

 

 

Unset environment variables.

Lastly, there's also a command to remove vars:

unset var example

unset $VARNAME command example.

  • Simply type "unset $VARNAME" to get rid of your undesired vars.

Of course, if your var has been declared as part of one of those files, just comment it out (with a hash-character) or yank the declarative code line.

Hopefully this is easy enough to get you started on the wonderful world of Linux environment variables.

Senior Professional Network and Computer Systems Engineer during work hours and father when home.

Andrea strives to deliver outstanding customer service and heaps of love towards his family.

In this Ad-sponsored space, Andrea shares his quest for "ultimate" IT knowledge, meticulously brought to you in an easy to read format.

Rate this post

Posted in LINUX, System Administration | No Comments »

Leave a Reply

Your email address will not be published. Required fields are marked *

Connect with Facebook