Skip to main content

The Shell Environment

Introduction

This section will cover some basic terminology and auxilliary commands that will be useful as we continue exploring use of the command line.

The Shell

So far, we have been working with the default shell on the SCRTP system, also known as bash. As mentioned at the beginning, bash is not the only shell available: the SCRTP machines also have cshell and tcshell. The commands in this section are often specific to bash. If you are using another shell, consult its documentation.

Programs and Processes

When you start a program, it is started in its own thread, or process. It is given an identifier or PID, and allocated resources such as memory. However, the new process is in some respects independent of its parent.

Environments

The bash shell you're working in has lots of variables and status information. For example, you can use the up arrow to scroll back through recent commands you have used. Typing 'history' will show a full list of recent commands in the current shell. Typing 'env' shows the currently set environment variables in the shell. For example, USERNAME is your username, and HOSTNAME is the computer you're using. These are inherited by any programs you start: for example a C or Python program can access them using a function getenv().

Note that there is a subtlety here: child processes inherit the environment when they start, but their changes don't "bubble-up" to the parent.

Environment variables in the shell are accessed (in normal commands) by pre-pending a '$' to their name, for example echo $USERNAME

They are set (note bash specific) using export like this (note no $ here, as this command is designed to work with these variables) export MYVAR=value

Removing a setting uses unset MYVAR

$PATH and $LD_LIBRARY_PATH

There is a special environment variable that you will probably encounter, called the PATH. This tells the system where to look for programs. The LD_LIBRARY_PATH tells the system where to find shared libraries used by a program when it starts up. Both are discussed in detail here.

Profile and Startup Files

Sometimes a program you use requires an environment variable be set before starting it, or needs a special addition to the path. You can do this every time, or you can add the command to a file that your shell runs each time it starts. The name of this varies slightly between systems, but it is always a hidden file (starts with a '.'). On the SCRTP it is called .bashrc and is in your home directory (cd ~ ).

Anything you put in this file will be executed by each new terminal window or tab you open. It wont affect already open processes though. You can (and should) always test what you're adding by running it in a shell first, and only add things you really want to apply to all your new terminals.

Some programs also use startup files: for example, vim configuration can be done using a .vimrc file. Consult the relevant program's info for more details.

Note: on OSX systems, the bash config file is usually called .bash_profile. If you're ever using a different shell, it will probably have its own config file.

Aliases

Finally, sometimes you want to use a particular command repeatedly and don't want to type it out every time. One way of doing this (advanced bash courses will introduce others) is to use an alias. The basic form of this is alias my_command="[command] [args] [etc]"

and to use it my_command [other args]

Note the new string is in quotes. The my_command string is simply replaced by the new string so:

  • Don't use names which collide with system functions or you will get strange errors. You can use which to check if a name is in use.
  • Check how your target command handles duplicate arguments. Many system commands reject an argument provided twice; some things take the last instance of an argument; etc.
  • Mostly aliases are only replaced if they're the first thing in a line.
  • Watch for stray spaces at the end of your string. Weird things can happen.
  • Aliases are replaced as strings, so you can include pipes etc within them. For example alias lstxt='ls | grep ".txt"' lists all .txt files in a single command.

As a final note, you may see suggestions, or be tempted, to e.g. override the rm command to add the -i (interactive) mode. This is fine, but risky: if you get into the habit of typing rm without thinking, you may get a surprise when on a machine that doesn't have the alias set. Consider instead defining and using your own name for it, for example rmi, so that the only surprise is a missing command.

Summary

This section mostly covered ways to avoid typing common things again and again. This helps avoid errors due to mistyping, as well as saving time. In short:

  1. Environment variables: useful to pass things around "implicitly". These are somewhat kin to global variables in programming languages, and come with similar caveats about changes having effects where you may not expect them, so tread carefully if changing values.
  2. Startup files: put common variables (and aliases, and path info) into a shell profile, so they're available in all your terminal windows.
  3. Aliases: use to define commonly used variations of commands you use, or their combinations.

Next Section

Watching Files and Processes