Software Development: Command-line Environment

[article] [edit page] [discussion] [history]

From Humanitarian-FOSS Project Development Site


Command-line Environment

For the initial assignments in this class, we will work under a command-line interface (CLI). This means either a Unix shell (GNU/Linux or OS X) or the Windows Command Prompt. In the former case I will assume that you are using the Bash shell, as that is the default on all major GNU/Linux systems and OS X; if you know enough to be using a different shell, then you probably already know how to configure paths, environment variables, etc.

Environment Variables

The following is from the Unix Tutorial by M. Stonebank, Copyright 2001:

In a CLI, variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Some are set by the system, others by you, yet others by the shell, or any program that loads another program.

Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables are visible both to the shell and to processes that are started by the shell. By convention, environment variables have UPPER CASE and shell variables have lower case names.

Setting environment variables in the Bash shell

To set an environment variable in the Bash shell, use the export command. For example, to set the CLASSPATH environment variable to /usr/local/java:., type

$ export CLASSPATH=/usr/local/java:.

CLASSPATH is now set in this shell and is avaailable to all processes that you start in this shell. However, it is not set in any other shells that you might start. To make that happen, add that line to the file .bashrc in your home directory (create the file if it does not exist). This is a file that is read by every Bash shell on startup, and all commands in it are executed. Note that you can also add to a variable be dereferencing it on the right-hand side of the assignment; for example, to add /opt/j2sdk_1_5/bin to the PATH environment variable (see below), type

$ export PATH=$PATH:/opt/j2sdk_1_5/bin

If PATH has the value /bin:/usr/bin:/usr/local/bin before this assignment, then it will have the value /bin:/usr/bin/:/usr/local/bin:/opt/j2sdk_1_5/bin afterwards.

Setting environment variables in Windows

To set an environment variable that applies to all Command Prompts in Windows, right-click on "My Computer," select "Properties," and then the "Advanced" tab. Click on the "Environment variables" button. There you will see a list of all the environment variables that will apply to newly-created Command Prompts. You can either modify existing variables or add new ones.

The Path

When you execute programs (e.g., javac or tomcat) in a CLI, the environment needs to find the executable program. To do so, it searches a list of directories known as the path. Usually the path is pre-configured with standard directories for executables. However, if you have to install software yourself, then those programs may not be put into those standard directories, and so you will have to add directories to your path. To do so, add the directories to the PATH environment variable. Be sure to add the directories as in the PATH example above (i.e., don't replace the existing path). For example, suppose you have installed the Java SDK under /opt/j2sdk_1_5. In this case, the executable programs (java, javac, etc.) will be located in /opt/j2sdk_1_5/bin, so this is the directory that you must add to your path, which is done as in the example above. Note that many modern GNU/Linux systems have much of the software that we will use available as packages, and those usually install the software in the standard locations, so you don't need to adjust your path at all.

There are a few minor differences between Unix-based systems and Windows for specifying the path. First a little terminology: a pathname is a specification of where to find a file on the filesystem. For example, the full pathname for the grep program on most Unix-based systems is /usr/bin/grep, indicating that it is located in the bin subdirectory of the usr directory of the root directory of the operating system. The path is then a list of pathnames that happen to be directories. On Unix-based systems, pathnames are specified using / to separate directories and : is used to separate entries in the path. Furthermore, directories should never have spaces in their names. On Windows, pathnames are specified using \ to separate directories and ; is used to separate entries in the path. Pathnames on Windows may also specify a drive and can have spaces in the directories; e.g., C:\Program Files\j2sdk_1_5\bin is a legal component of the path on Windows.

Personal tools