Hello Linux-fanactics, this is the part 2 of BASH Scripting series. In part1 of the series, we introduced you to BASH & BASH scripting . In this post we will be discussing Environment Variables, theirs uses & how we can define them.

Firstly, what is a variable ?

It is basically a location for storing a value, it can be a number, a letter or a filename.

Environment  Variables

Environment  Variables in Linux stores information for shell sessions & the working environment. These variables can be accessed by any program or a script (hence we need to know it for BASH scripting). Its good way to store frequently used data.

Environment variables are of two types:-

  1. Global variables          These can be accessed from any shell sessions,
  2. Local variables             These are only available to the shell that created it

To see a list of all variables, use “set ” command and to view value of a single variable , we use “printenv variable_name”.

set command

 

Creating user-defined Local variables

We can create local-variables ourself & to do so is very easy. See example below

$ new_variable=”WELCOME HERE”                                                             (NO SPACES)

That is how a local variable is created and it can only be used in our current shell session.

 

Creating user defined Global variable

To create a global variable, we first create a local variable & than use “export” command to make it a global variable.

$ new1_variable=”This is 2nd variable”
$ export new1_variable

Now, this user created global variable can be used in all shell sessions.

 

Removing Environment variables

To remove an environment variable, we use “unset” command,

$ unset new_variable

and  the variable will no longer be an environment variable.

 

Using 'PATH' Environment variable

As mentioned above, “set” command provides all the environment variables which we can than also use in our scripts (much on that in later posts).

One of the most useful & environment variable is “PATH”. It is used to store a command’s or program’s default location ,

path command

We can also use it store a directory with scripts & when its done, rather than using whole path to start a script we can just use name of the script.

For example, /home/test has a number of script & one of the script name is testing.sh. If we use script name to run the script, we get

$ testing.sh
-bash: testing.sh: command not found
$

But if we use PATH command to declare the path of the directory with scripts,

$ PATH=$PATH:/home/test

Now if try to run script just by its name , we get

$ testing.sh
This is testing script.
$

It’s very handy right !!!

 

So, this was my Part 2 of Scripting  tutorial, here we learned how we can create environment variables, in our next tutorial we will learn to use them in our scripts as well.

In meanwhile, send in your queries/feedback using comment box below.

 

If you think we have helped you or just want to support us, please consider these :-

Connect to us: Facebook | Twitter | Google Plus

Become a Supporter - Make a contribution via PayPal

[paypal_donation_button align="left" border="1"]

Linux TechLab is thankful for your continued support.