Unix - Shell Environment Variables and Profiles in UNIX

Shell environment variables and profile files are fundamental components of UNIX systems. They help customize the user environment, control the behavior of commands and applications, and automate settings that are loaded whenever a user logs in or starts a shell session.

Understanding environment variables and profile files is essential for system administrators, developers, and regular users because they influence how the operating system and applications function.

What Are Environment Variables?

Environment variables are named values stored by the shell that provide information to programs and scripts running in a UNIX environment.

They act as configuration settings that can be accessed by the shell and various applications.

Example

USER=alice
HOME=/home/alice
PATH=/usr/bin:/bin:/usr/local/bin

In this example:

  • USER stores the username.

  • HOME stores the user's home directory.

  • PATH stores directories where executable commands are searched.

Applications use these variables to determine locations, preferences, and operational settings.

Types of Variables

Local Variables

Local variables exist only within the current shell session.

Example:

course="UNIX Training"
echo $course

Output:

UNIX Training

This variable is not available to child processes.

Environment Variables

Environment variables are exported and inherited by child processes.

Example:

export course="UNIX Training"

Now any program launched from this shell can access the variable.

Viewing Environment Variables

Display All Environment Variables

env

or

printenv

Example output:

HOME=/home/user
USER=user
PATH=/usr/bin:/bin
SHELL=/bin/bash

View a Specific Variable

echo $HOME

Output:

/home/user

Common Environment Variables

HOME

Specifies the user's home directory.

echo $HOME

Example:

/home/student

USER

Stores the current username.

echo $USER

PATH

Contains directories searched for executable commands.

echo $PATH

Example:

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

SHELL

Displays the current shell.

echo $SHELL

Example:

/bin/bash

HOSTNAME

Displays the system hostname.

echo $HOSTNAME

PWD

Shows the current working directory.

echo $PWD

Creating Environment Variables

Temporary Variable

EDITOR=vim

This variable exists only during the current session.

Exporting a Variable

export EDITOR=vim

This makes the variable available to child processes.

Checking the Variable

echo $EDITOR

Output:

vim

Removing Variables

Variables can be deleted using the unset command.

Example:

unset EDITOR

Verify:

echo $EDITOR

No value will be displayed.

The PATH Variable

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

When a user enters a command, the shell searches directories listed in PATH to locate the executable.

Example:

PATH=/usr/local/bin:/usr/bin:/bin

When you type:

ls

The shell checks each directory in PATH until it finds the ls executable.

Adding a Directory to PATH

export PATH=$PATH:/home/user/scripts

This appends a custom scripts directory to the existing PATH.

Verify Changes

echo $PATH

Shell Profile Files

Profile files contain commands and settings executed automatically when a shell session starts.

These files help users customize their environment without manually entering commands each time they log in.

Important Profile Files

/etc/profile

System-wide configuration file.

  • Applies to all users.

  • Executed during login shell startup.

  • Usually managed by administrators.

Example settings:

PATH=/usr/bin:/bin
export PATH

~/.profile

User-specific login configuration.

Located in the user's home directory.

Example:

export EDITOR=vim
export LANG=en_US.UTF-8

~/.bash_profile

Used specifically by Bash login shells.

Example:

export PATH=$PATH:$HOME/bin

~/.bashrc

Executed whenever a new Bash shell is opened.

Typically used for:

  • Aliases

  • Functions

  • Shell options

  • Environment variables

Example:

alias ll='ls -l'
alias la='ls -a'

~/.bash_logout

Executed when the user logs out.

Example:

clear

Difference Between .bash_profile and .bashrc

Feature .bash_profile .bashrc
Executed during login Yes No
Executed for new terminal windows Sometimes Yes
Stores environment settings Yes Yes
Stores aliases and functions Rarely Commonly

A common practice is to call .bashrc from .bash_profile.

Example:

if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

This ensures consistent settings across login and non-login shells.

Shell Aliases

Aliases provide shortcuts for frequently used commands.

Example:

alias ll='ls -l'

Usage:

ll

Instead of typing:

ls -l

Display Aliases

alias

Remove an Alias

unalias ll

Shell Functions

Functions allow users to create reusable command blocks.

Example:

backup() {
    cp $1 $1.bak
}

Usage:

backup file.txt

Result:

file.txt.bak

Functions are commonly stored in .bashrc.

Setting Prompt Variables

The shell prompt can be customized using the PS1 variable.

Example:

PS1="\u@\h:\w$ "

Output:

user@server:/home/user$

Components:

  • \u = Username

  • \h = Hostname

  • \w = Current directory

Reloading Profile Files

After modifying profile files, changes can be applied without logging out.

Example:

source ~/.bashrc

or

. ~/.bashrc

This reloads the file immediately.

Environment Variables in Shell Scripts

Variables can be used inside scripts.

Example:

#!/bin/bash

echo "User: $USER"
echo "Home: $HOME"
echo "Shell: $SHELL"

Output:

User: alice
Home: /home/alice
Shell: /bin/bash

Best Practices

  1. Store user-specific settings in .bashrc or .bash_profile.

  2. Avoid modifying system-wide files unless necessary.

  3. Use meaningful variable names.

  4. Keep PATH organized and avoid duplicate entries.

  5. Comment profile files for easier maintenance.

  6. Use aliases sparingly to avoid confusion.

  7. Test changes in a temporary shell before making them permanent.

  8. Secure profile files by assigning appropriate permissions.

Conclusion

Shell environment variables and profile files form the foundation of user customization in UNIX. Environment variables provide configuration data to shells and applications, while profile files automate the setup of user environments during login or shell startup. Mastering variables such as PATH, HOME, and SHELL, along with files like .profile, .bash_profile, and .bashrc, enables users to create efficient, personalized, and productive UNIX working environments.