Unix - UNIX Shell Environment Variables

UNIX shell environment variables are named values maintained by the shell and operating system that define the behavior of the user session, programs, and scripts. They act as a communication layer between the shell and applications. These variables store configuration information such as user details, file paths, language settings, and command execution preferences. Whenever a user logs in or opens a terminal, the shell loads several predefined environment variables to set up the working environment.

Environment variables are essential because many commands and programs rely on them to function correctly. They provide information to processes without requiring users to manually specify settings every time. For example, when a user types a command, the shell checks the PATH variable to know where to search for executable files. Without environment variables, each program would need explicit configuration for directories, user settings, and system options.

Types of Variables in UNIX Shell

There are mainly two types of variables in UNIX:

Shell Variables
These are local to the current shell session. They are created and used only within the shell in which they are defined. They are not passed to child processes unless explicitly exported.

Environment Variables
These are variables that are available not only in the current shell but also inherited by child processes. Programs and scripts launched from the shell can access them.

For example:

name="unix"
export name

In this case, name becomes an environment variable after using the export command.

Common UNIX Environment Variables

Several environment variables are used frequently in UNIX systems.

PATH
This variable contains a list of directories separated by colons. When a command is entered, the shell searches these directories to find the executable.

Example:

echo $PATH

Output may look like:

/usr/local/bin:/usr/bin:/bin:/usr/sbin

This means the shell will search these directories in order when locating commands.

HOME
The HOME variable stores the path to the current user's home directory.

Example:

echo $HOME

Output:

/home/user

This directory is used as the default location when the user logs in.

USER
Stores the username of the current session.

Example:

echo $USER

Output:

john

PWD
Represents the present working directory.

Example:

echo $PWD

Output:

/home/user/documents

SHELL
Indicates the path of the current shell program.

Example:

echo $SHELL

Output:

/bin/bash

Creating Environment Variables

Users can create their own environment variables to store custom settings.

Syntax:

variable_name=value

Example:

project=/home/user/project1

To access it:

echo $project

Output:

/home/user/project1

To make it available to child processes:

export project

Now any program started from the shell can read this variable.

Viewing Environment Variables

UNIX provides commands to display environment variables.

printenv

This command shows all environment variables.

printenv

It prints the list of variable names and their values.

env

Another command to display environment variables.

env

set

Displays shell variables along with environment variables.

set

This command gives a more extensive list.

Modifying Environment Variables

Existing variables can be changed by assigning a new value.

Example:

PATH=/usr/local/bin:$PATH

This adds a new directory at the beginning of the PATH.

The shell will search /usr/local/bin first before other directories.

This technique is useful when installing custom programs.

Removing Environment Variables

Variables can be deleted using the unset command.

Example:

unset project

After this, the variable no longer exists in the current session.

To verify:

echo $project

No output will appear.

Exporting Variables

The export command makes shell variables accessible to child processes.

Example:

name="UNIX"
export name

Now if a shell script or child shell is launched, it can use the name variable.

Without exporting, child processes will not recognize it.

Startup Files for Environment Variables

To make environment variables permanent, they are stored in startup configuration files.

Common files include:

.profile
Used for login shell initialization.

.bash_profile
Used specifically for Bash login sessions.

.bashrc
Used for interactive shell sessions.

.zshrc
Used for Z shell configurations.

Example entry in .bashrc:

export JAVA_HOME=/usr/lib/jvm/java

After saving the file, apply changes using:

source ~/.bashrc

This reloads the file without restarting the terminal.

Role of PATH Variable

The PATH variable is one of the most important environment variables.

When a user enters:

ls

The shell checks the directories listed in PATH to find the ls executable.

If PATH is incorrect, commands may fail with:

command not found

Administrators often modify PATH to include application directories.

Environment Variables in Scripts

Shell scripts frequently use environment variables to access system information.

Example script:

#!/bin/bash
echo "User: $USER"
echo "Home: $HOME"
echo "Current Directory: $PWD"

Output:

User: john
Home: /home/john
Current Directory: /home/john/projects

This makes scripts adaptable across different users and systems.

Special Variables

UNIX shells provide special built-in variables.

$?
Stores the exit status of the last command.

Example:

ls
echo $?

0 means success.

$$
Stores the process ID of the current shell.

Example:

echo $$

$0
Stores the shell or script name.

$#
Stores the number of command-line arguments.

$*
Represents all arguments passed to a script.

These variables help in script control and debugging.

Advantages of Environment Variables

Environment variables provide many benefits.

They simplify user configuration by storing common paths and preferences. Programs can adapt based on user settings. Scripts become portable because they can read values from the environment. System administration becomes easier because changes can be centralized in startup files.

They also improve automation. A script can detect user identity, home directory, and system configuration without manual input.

Security Considerations

Improperly configured environment variables can cause security problems.

For example, if PATH contains insecure directories, malicious programs may execute instead of trusted system commands. Sensitive data stored in environment variables can sometimes be viewed by processes or users.

Administrators must ensure that variables are properly controlled and not exposed unnecessarily.

Practical Example

Suppose a developer installs a custom compiler in /opt/tools/bin.

Instead of typing the full path:

/opt/tools/bin/compiler

They can add the directory to PATH:

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

Now they simply run:

compiler

This improves convenience and productivity.

Conclusion

UNIX shell environment variables are a fundamental part of system operation. They define how the shell behaves, how commands are executed, and how programs access system information. Variables like PATH, HOME, USER, and SHELL are used constantly in daily operations. By understanding how to create, modify, export, and manage environment variables, users can customize their shell environment, automate tasks, and improve efficiency in UNIX systems.