Linux Basics

Learn the basics of operating a Linux-based operating system (OS) and take your first steps in exploitation in a Linux environment!

Easy

Linux Basics
Environment Variables

Linux_Basics::Environment_Variables

Variables are containers of information used to store values for future use, and should be a familiar concept to those with programming experience. Linux shells and even the operating systems also implement their own type of variables.

These are generally split into two categories, shell variables and environment variables.

Shell Variables

To refresh, the shell is the program which allows u to type commands to run programs and perform simple operations. So far, the shell that we have been using in this module is bash.

Shell variables are considered local variables, and only affect and exist in the current session of the shell being used. If you restart the shell, or start a new session separately, the variables previously defined will not exist anymore.

Setting shell variables is very straightforward, simply use the following syntax:

VARIABLE_NAME=VARIABLE_VALUE

You may optionally wrap the VARIABLE_VALUE in quotes as well, which is useful when your value has characters that may be interpreted by the shell, like spaces or semicolons.

VARIABLE_NAME="VARIABLE VALUE"

After you've defined the variable, you can simply reference it in the commandline with the syntax $VARIABLE_NAME (note the $ prefix). Here is an example usage of variables:

$ LESSON="Linux Basics"
$ echo "I am working on the $LESSON lesson!"
I am working on the Linux Basics lesson!

To observe the that these variables only exist in the current session, we can try to start a new bash session and notice what happens to our previously defined variables!

$ VAR_EXISTS="Hi I exist!"
$ echo $VAR_EXISTS
Hi I exist!

# Start a new sesson of our shell (bash)
$ bash
$ echo $VAR_EXISTS

# there is no output in the line above ^

Notice that the second time we try to echo the value of $VAR_EXISTS, we get an empty output? That's because the local shell variables we've defined this way do not carry over to a new shell session we start with bash. In fact, any other program that we start will not have access to this variable.

So what if we do want these variables to propagate to other programs that we start? That's where environment variables come in!

Environment Variables

As we've established, setting local shell variables does not make them environment variables, thus other programs being run will not have these variables passed to them. We can test this by using the env command that lists environment variables!

$ HELLO="Hi there!"
$ env | grep "HELLO"

# there is no output in the line above ^

As you can see the HELLO shell variable is not yet an environment variable. To set it as an environment variable simply use the export command with the following syntax:

export <VARIABLE_NAME>

We can now redo the previous example, but now exporting the HELLO variable as an environment variable too.

$ HELLO="Hi there!"
$ export HELLO
$ env | grep "HELLO"
HELLO=Hi there!

Now we see it listed as an environment variable! We can now test whether it will be transferred to other programs that we run from this session.

$ HELLO="Hi there!"
$ export HELLO

$ bash
$ echo $HELLO
Hi there!

It works!

Quiz

I run the following command:
export OMU="Cool!"
Is this variable an environment variable?

There is a simpler way of declaring environment variables, and this way is more commonly used.

$ export HELLO="Hi there!"
$ echo $HELLO
Hi there!

Variable Permanence

However, it is important to understand that even environment variables are not permanent. If you close the terminal session and start a new one, these environment variables will not exist anymore. This is because environment variables are only propagated to child processes, i.e. processes that have been spawned by the session you created.

For example, if you run ls inside your bash terminal, then the ls process will be a child process of your bash process. Thus the ls process will inherit the environment variables.

bash
└── ls

However, parent processes, or processes run by other means will not inherit the environment variables. Here is an example, try to understand the output as it may be confusing (keep track of the sessions!)

(A) $ MY_SESSION=Alice
(A) $ echo $MY_SESSION
    Alice
(A) $ export MY_SESSION

(A) $ bash
(B) $ echo $MY_SESSION
    Alice
(B) $ MY_SESSION=Bob
(B) $ echo $MY_SESSION
    Bob
(B) $ export MY_SESSION
(B) $ exit

(A) $ echo $MY_SESSION
    Alice

In this example, the A session of bash spawns a child session B, exporting the variable MY_SESSION.

bash (A)
└── bash (B)

Observe how even though B exports the newly defined value of MY_SESSION=B, when we exit back to the A session of bash, it does not have this new value of MY_SESSION, and still uses the original value A. This is because the environment variables of the child B will not be propagated to the parent A.

If you do want your environment variables to be permanent, one common way is to make use of the ~/.bashrc file (remember that ~ is a shortcut for the home directory of the current user). The ~/.bashrc file is a special file that contains many bash commands. At the start of any bash session, these commands will be run before the user is given control. This is thus very useful to set-up user-wide configuration.

Lets give it a try!

$ echo 'export PERMANENT="I exist in every bash session by this user!"' >> ~/.bashrc

Now if we start a new session of bash (by closing the terminal and opening it again)

$ echo $PERMANENT
I exist in every bash session by this user!

It works! This is a popular way of setting configuration to ease the use of the shell for the user. You can try reading the ~/.bashrc file, or list some of the environment variables you have with printenv. Try googling some of these environment variables to see how they are useful for you!

Try checking out some useful variables like PATH or SHELL