Skip to content

Unix / Linux - Environment

The shell environment encompasses the settings and variables that define your command-line session’s behavior and context. Understanding and customizing this environment is key to efficient use of Unix-like systems.

Variables store data (text, numbers, paths) under a name.

  • Shell Variables: Exist only within the current shell session. They are not passed down to programs started from that shell.
  • Environment Variables: Are inherited by child processes (programs or subshells) started from the current shell. They define the broader execution environment.

Setting variables (syntax varies slightly between shells like bash, zsh, sh):

# Set a SHELL variable (common syntax for Bourne-like shells: bash, zsh, sh)
# NO spaces around the '=' !
MY_VAR="Hello Shell"
# Access the variable's value using '$'
$ echo $MY_VAR
Hello Shell
# Make the shell variable an ENVIRONMENT variable using 'export'
export MY_VAR
# Or set and export in one step
export EDITOR="vim"

To see all environment variables currently set:

$ env
# Or
$ printenv

To see a specific environment variable’s value:

$ echo $HOME
/home/user
$ printenv PATH
/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/home/user/bin

When you log in or start a new shell session, the shell reads configuration files to set up the environment, define aliases, functions, and variables. The specific files read depend on the shell (e.g., bash, zsh) and whether it’s a login shell or an interactive non-login shell.

Common configuration files include:

  • System-wide settings: Often in /etc/profile, /etc/bash.bashrc (for bash), /etc/zsh/zshrc (for zsh). Managed by the system administrator.
  • User-specific settings (in your home directory ~):
  • Bash: Reads ~/.bash_profile, ~/.bash_login, or ~/.profile (first one found) for login shells. Reads ~/.bashrc for interactive non-login shells (most terminal windows you open after login). Often, .bash_profile is set up to simply load .bashrc.
  • Zsh: Reads ~/.zshenv (always), ~/.zprofile (login), ~/.zshrc (interactive), ~/.zlogin (login after zshrc). Most user customizations go in ~/.zshrc.
  • Traditional Bourne Shell (sh): Reads /etc/profile then ~/.profile for login shells.

General Practice: Put most of your personal environment settings (PATH modifications, aliases, custom functions, prompt settings) in ~/.bashrc (for Bash) or ~/.zshrc (for Zsh). Settings needed specifically at login time (which might be rare) go in ~/.bash_profile or ~/.zprofile.

Several standard environment variables significantly affect shell behavior.

A colon-separated list of directories (:). When you type a command (e.g., ls), the shell searches these directories in order to find the executable file. If not found in any directory in the PATH, you get a ‘command not found’ error.

$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/home/user/.local/bin:/home/user/bin

To add your own custom script directory (~/scripts) to the PATH (add this line to your ~/.bashrc or ~/.zshrc):

export PATH="$HOME/scripts:$PATH"

(Prepending ensures your scripts are found before system ones with the same name, if any. Appending ($PATH:$HOME/scripts) is also common).

The absolute path to your home directory.

$ echo $HOME
/home/alice

Your login username.

$ echo $USER
alice

The path to your default login shell program.

$ echo $SHELL
/bin/bash

Specifies the type of terminal emulator you are using (e.g., xterm, xterm-256color, linux, screen). Programs use this to determine how to draw output correctly (e.g., using colors, clearing the screen).

$ echo $TERM
xterm-256color

Incorrect TERM settings can lead to garbled output in programs like vim or less.

Specifies the default text editor to be used by other programs (e.g., git for commit messages, crontab -e). VISUAL usually takes precedence for full-screen editors, EDITOR for line editors.

# Set in ~/.bashrc or ~/.zshrc
export VISUAL="vim"
export EDITOR="nano"

The current working directory. Updated automatically by the shell when you use cd.

$ pwd
/home/alice/projects
$ echo $PWD
/home/alice/projects

PS1 defines the primary command prompt string. It can contain special escape sequences to display information like username, hostname, current directory, time, etc. Customizing PS1 is a common way to personalize the shell.

PS2 defines the secondary prompt, shown when the shell expects more input (e.g., after an opening quote or parenthesis).

Example PS1 setting for Bash/Zsh (often set in .bashrc/.zshrc):

# Displays [user@hostname current_directory]$ (or # for root)
export PS1='[\u@\h \W]\$ '

Modern prompts often include colors and Git branch information, which involves more complex PS1 settings.

Other variables like LANG, LC_* control language and localization settings. LD_LIBRARY_PATH influences dynamic linking (use with caution).

Modifying environment variables in your shell configuration files allows you to tailor the Unix/Linux command-line experience to your needs.