Shell Variables   «Prev  Next»
Lesson 3 Using predefined system variables
Objective Access system information using standard shell variables.

Using Predefined System Variables

Question: How do I access system information using standard shell variables in a Unix Shell Script?
There are a number of standard shell variables that can be used to access system information in a Unix shell script. These variables are set by the operating system and contain information such as the system name, hostname, kernel version, and current user.
Some of the most commonly used standard shell variables for accessing system information include:
  1. $HOSTNAME: The hostname of the system.
  2. $OSTYPE`: The operating system type.
  3. $KERNEL_VERSION`: The kernel version.
  4. $USER`: The current user.
  5. $PWD`: The current working directory. To access these variables in a shell script, you can simply use the dollar sign () followed by the variable name.

For example, to print the hostname of the system, you would use the following code:
echo "The hostname of the system is $HOSTNAME"

This code would print the hostname of the system to the console.
Here is a list of some of the other standard shell variables that can be used to access system information:
  1. $MACHTYPE: The machine type.
  2. $ARCH: The architecture.
  3. $RELEASE: The operating system release.
  4. $VERSION: The operating system version.
  5. $DIST: The distribution name.

Here is an example of a shell script that uses standard shell variables to access system information:

# Print the hostname of the system
echo "The hostname of the system is $HOSTNAME"

# Print the operating system type
echo "The operating system type is $OSTYPE"

# Print the kernel version
echo "The kernel version is $KERNEL_VERSION"

# Print the current user
echo "The current user is $USER"

# Print the current working directory
echo "The current working directory is $PWD"

To run this script, you would save it as a file with the .sh extension and then run it from the command line.
For example, if you saved the script as, you would run it by typing the following command:

This would print the system information to the console.

Shell Script execution Environment

Every shell script is executed in an environment that includes the operating system, the user settings, the resources available, the network settings, and other information. Depending on the purpose of the script, some of these details may be relevant to how a script operates.
Shell scripts can access many different pieces of information about their environment. Many of these are related to UNIX process information or to commands. This information, when accessed by a script, can help a shell script determine which actions to take.

Checking Number of command line Arguments

One type of information that can be accessed by the script is the number of parameters on the command line. For example, if the task you have designed for your script to complete requires that the user supply two parameters on the command line, you can test the value of the $# variable to see how many parameters were supplied when the script was launched. The $# variable holds the number of parameters on the command line that started the script. The test might look like this command (followed by other commands acting on this test):
if [ $# = 2 ];

The shell places each of the parameters from the command line into a positional variable as the script is launched.
You can then access these variables (such as $1, $2, etc.) in your script.

Checking System Variables

Another type of information that can be accessed by the script is the UNIX process ID number. If you need to know the UNIX process ID number of the shell script (perhaps to use it as a unique temporary filename), you can reference the $$ variable.
The shell script can also access system variables.

Many of the special that are set by the shell when you run a script are like environment variables, but they are used to hold information about your computer system rather than information about specific programs. One example is the OSTYPE variable.
By testing the value of this variable with an if/then statement, your shell script can determine where to look for files based on the operating system that the script is run on. For example, if your script determines that the OSTYPE is Linux, the script might use different commands than if the script is being run on an HP/UX operating system.
The following MouseOver applet shows a small script that uses two different system variables to convert a database file using proprietary programs called convertdb and filterdb. The script creates an intermediate temporary file using a system variable.

shel script description
  1. Describe the script. Comments should also include what command line arguments the script requires, author, date of creation, etc.
  2. Test the value of the $# variable to determine how many items were on the command line when the script was started. This script does not test the validity of the values, only that the correct number of items are included.
  3. If the test for "not equal to 2" succeeded, the user running the script has not provided the needed information. Use the echo command to print a usage message and then stop the script with the exit command.
  4. End the block of commands that are executed when the if/then test returns true. In this case, the $# variable indicates that the incorrect number of command line arguments were included.
  5. Run the database utility called convertdb, using the command line parameters entered by the user running the script as values for the utility. The values are accessed as $1 and $2 and placed in the command line where convertdb requires them. Write out the resulting file to a new temporary file called tmp with the process ID[1] of the script as a file extension. Using the PID as a file extension simply creates a unique filename for temporary use. (For example, the temporary file might be called tmp.941.)
  6. Run the filterdb utility on the temporary file created in the previous step using the convertdb program. Write the results back to the original filename provided by the user. That filename is stored in the variable $1.

System Variables in Sample Script
Accessing environment variables is the subject of the next lesson.
[1] Process ID number: A unique number assigned by the UNIX kernel to each task (process) running on the system.