Unix / Linux - Environment
Unix / Linux - The Shell Environment
Section titled “Unix / Linux - The Shell 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.
Shell Variables vs. Environment Variables
Section titled “Shell Variables vs. Environment Variables”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_VARHello Shell
# Make the shell variable an ENVIRONMENT variable using 'export'export MY_VAR
# Or set and export in one stepexport EDITOR="vim"To see all environment variables currently set:
$ env# Or$ printenvTo 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/binShell Initialization Files
Section titled “Shell Initialization Files”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~/.bashrcfor interactive non-login shells (most terminal windows you open after login). Often,.bash_profileis set up to simply load.bashrc. - Zsh: Reads
~/.zshenv(always),~/.zprofile(login),~/.zshrc(interactive),~/.zlogin(login afterzshrc). Most user customizations go in~/.zshrc. - Traditional Bourne Shell (
sh): Reads/etc/profilethen~/.profilefor 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.
Key Environment Variables
Section titled “Key Environment Variables”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/binTo 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/aliceUSER / LOGNAME
Section titled “USER / LOGNAME”Your login username.
$ echo $USERaliceThe path to your default login shell program.
$ echo $SHELL/bin/bashSpecifies 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 $TERMxterm-256colorIncorrect TERM settings can lead to garbled output in programs like vim or less.
EDITOR / VISUAL
Section titled “EDITOR / VISUAL”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 ~/.zshrcexport 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/projectsPS1 / PS2 (Prompt Strings)
Section titled “PS1 / PS2 (Prompt Strings)”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.