Variables are, strictly speaking, part of Bash and the shell - something we'll explore in the next chapter (smile). However, it makes sense sequentially to introduce them first here

One natural thing to want to do in any language is to try to store information in variables, and Linux is no different. Variables can take any name that consist of letters, digits, and underscores (although they cannot begin with digits). By convention, we will only use uppercase letters. 

Storing Values to Variables

To set an environment variable, we can assign it with the = symbol, much like other languages. Assignments take the form:

VAR=VALUE

This includes VAR (the name of the variable), VALUE (the value we want to give it), and the equal sign. In Linux, all values are stored as text, or "strings", so we don't need to worry about types

For values without spaces, we can simply type them as is after the =. However, if the text we want to assign has spaces, we need to surround it with either single or double quotations, to indicate that all of it is the string we want to assign, and not just spaces in our command. It is never incorrect to surround the value with quotations

Try storing a hello message to a variable named HELLO like so:

HELLO="Hello, C2S2 Coder"

If you ever want to see all of the defined variables, you can run the set command. In addition, if you ever want to undefine a variable (effectively deleting it), you can run unset VAR, where VAR is the name of the variable you want to undefine

Accessing Values in Variables

To access the value stored in a variable, we simply prefix the variable name with a $:

$VAR

Note that this directly replaces the instance of the variable with the string that you assigned to it. For example, if you were to directly type $HELLO , you'd get an error somewhat along the lines of Hello,: command not found. This is because we're trying to directly execute the "command" Hello, C2S2 Coder - Linux sees the first word Hello, (which usually specifies the command), and doesn't understand it. Instead, if we want to see the information stored in a variable, we usually give it to echo:

echo $HELLO

Running the code above, the terminal should print out Hello, C2S2 Coder. This shows that we've successfully stored the text in the HELLO variable (smile). Now, some among you might think "Hey, can we store complicated commands in variables?" We could, but there is a better way: aliases

Creating Aliases for Commands

In Linux, another useful way to store information is through aliases. These are very similar to variables, but are meant instead to store long commands in a shorter form. Typically, they are lowercase, contrasting from variables (as many commands are lowercase), and are used on their own (as opposed to variables, which can be used within commands, as we saw with echo.

To assign to them is the same as assigning variables, except that we prefix the assignment with the alias command:

alias CMD=LONG_CMD

For example, if we want to create a command to perform our printing from before, you could do so with:

alias c2s2_echo="echo $HELLO"

Now, we can perform the same task by simply running

c2s2_echo

Verify that you can perform this as well, indicating that you can correctly alias commands

Many users perform ls -l and ls -a frequently, and accordingly alias them to ll and la, respectively. See if you can do this on your terminal, and verify by running ll and la 

Environment Variables

While we can define variables, our environment pre-defines a few for us to be helpful! Some of the more useful ones are:

  • HOME: Contains the path to your home directory
  • USER: The username of the current user (fun fact: this can also be obtained with the command whoami)
  • PWD: The path to your current working directory (the same one returned by the pwd command)
  • $: The Process ID of the process (something we'll explore in Chapter 6!)

If you want to see the full list of pre-defined variables, you can see them by running the env command.

The PATH variable

There is one environment variable that is probably the most important, and the most used; PATH. Let's look at what it contains:

echo $PATH

Running this, we can see that it is a list of different paths, separated by colons. To understand this, we need to understand what happens we type commands into Linux. Unfortunately, Linux doesn't have magical knowledge of what to do when we type in a string of letters (with the exception of built-in shell commands - we'll discuss what a shell is in the next chapter). Instead, it goes and executes a pre-made executable for that command. For example, when we run ls, under the hood, we are running an ls executable that exists somewhere. If you want to see where an executable for a given command CMD lives, you can run which CMD: (many of them are stored in a directory titled bin, which stands for binary, as these are pre-compiled, binary executables)

which ls

Similarly, Linux doesn't magically know where to find these executables. Instead, it searches all the paths stored in the PATH variable to find the executable. Therefore, the directories listed in PATH define all of the executables that we're able to run as commands from the terminal.

When we're running an executable that's found in our PATH , we don't need to specify it with the relative path, as we needed to for previous executables. This is because the relative path is no longer too relevant when using PATH, to distinguish running these commands from running executables in our current directory, as well as to be able to write them nicely on the command line without needing the ./ (smile)

Because of this, we can add to the PATH variable to allow it to find our own executables! For example, imagine we have a directory /home/<netid>/executables which contains an executable named my_exec. We can add the directory that contains it to our path, separating it with a colon:

PATH=$PATH:/home/<netid>/executables

Now, we could run our my_exec executable (as well as any other executables in /home/<netid>/executables) from anywhere in our directory structure by just running the command my_exec, as Linux will find the executable for it in the directories in our PATH (smile)

Ordering in your PATH does matter! The paths in PATH are searched in order; this means that if you ever have two programs with the same name, the one that is in the directory that occurs first in PATH will be chosen.

  • No labels