Last time we looked at bash, the standard Unix shell found on Linux and OSX systems.
Although it is often seen as old-fashioned, the shell can make certain tasks easier and faster to perform than loading up a GUI application to do the same, and it is definitely worth getting to know.
This time we will look a little more at the shell, covering a few advanced features.
Environment variables
A shell is essentially an interpreter for running commands. It can operate
interactively at a prompt or run through a programmed sequence of commands,
known as a shell script. In both states the shell maintains a unique
environment. If you run a separate shell in another terminal it will have its
own environment.
In programming languages such as Basic and C you will have heard of variables names that can hold a value. Bash has the same, but known as environment variables.
An environment variable is identified by a straightforward name, such as USER or MYVAR. It is not necessary for the identifier to be in capitals, but this is the universal standard, as it makes clear you are talking about a variable and not a command name. When you want to use the value of an environment variable, the name is prefixed with a dollar sign; thus the contents of USER is expressed with $USER.
The shell automatically sets up a few environment variables when it starts up. One of these is USER, which holds the name of the user that ran the shell. Another is SHELL, which contains the name of the shell. HOME is set to the home directory of the current user. Each of these can be shown by running ‘echo’. This usually prints something to the terminal. For example, ‘echo hello’ will print ‘hello’ on the terminal. Printing an environment variable is just as easy:
$ echo $USER
barry
$ echo $HOME
/home/barry
What happens here is that the shell substitutes whatever is held in the environment variable and sends the result to the echo command, as if you had typed it yourself. The distinction is important the echo command does not have to process the environment variable itself.
These variables are commonly used in shell scripts. A script could be run by any user, so the script could, for example, check the contents of USER to determine if it is being run as root or not.
The PATH variable
When a bash shell starts up it runs through a few scripts automatically. One of
these is /etc/profile, a system-wide script for setting up the shell
environment. The PATH variable is typically set here, either directly or
indirectly. This is one of the most important environment variables, as it
determines how commands can be run.
As we saw last month, if you run ‘ls -l’ in the shell, the interpreter finds the ‘ls’ command on the filesystem and executes it with the argument ‘-l’. It does not, of course, have to guess where to find the command. Instead it looks at the PATH variable, and searches each directory in turn for an executable command called ls. It will execute the first one it finds, or if after searching every directory, if can’t find ‘ls’, it will return a ‘command not found’ error. Have a look at the PATH variable with echo:
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
This is the standard path for Ubuntu Linux. When typing a command, such as ls, the shell first checks /usr/local/sbin, and proceeds through the rest, with each directory separated by a colon.
All PC Operating SystemsTags: Windows
