NANDHOO.

Chapter 4: The Environment

Your shell session is not just a blank slate; it is a dynamic environment populated with variables, aliases, and settings that define how you interact with the system. Mastering the environment is the first step toward building a personalized, efficient workflow.

I. Global vs. Local Variables

The shell distinguishes between variables that are local to the current session and those that are "exported" to child processes.

TypeScopeDefinitionAccess
Shell VariableLocalMY_VAR="hello"Only current shell
Environment VariableGlobalexport MY_VAR="hello"Current shell + all sub-processes

Common Environment Variables:

  • $USER: The current logged-in username.
  • $HOME: Path to your home directory.
  • $SHELL: The path to the shell binary you are using.
  • $EDITOR: Your preferred text editor (e.g., vim, nano).
  • $LANG: The system's language and character encoding (e.g., en_US.UTF-8).

II. The PATH: A System-Wide Map

The most critical environment variable is $PATH. It is a colon-separated list of directories where the shell looks for executable files.

"node"$PATH List1. /usr/local/bin2. /usr/bin3. /bin4. /home/jane/binMATCH FOUND/usr/bin/node

III. Shell Configuration (Dotfiles)

To make settings permanent, you add them to configuration files in your home directory.

  1. Login Shells (e.g., SSH, initial login):
    • Reads ~/.bash_profile or ~/.zprofile.
  2. Non-Login Shells (e.g., opening a new terminal tab):
    • Reads ~/.bashrc or ~/.zshrc.

Pro Tip:

Always put your export commands and alias definitions in ~/.bashrc (for Bash) or ~/.zshrc (for Zsh) to ensure they are available everywhere.

IV. Customizing the Prompt (PS1)

The text you see before your cursor is controlled by the $PS1 variable. You can use special escape characters to show the time, user, or current directory.

EscapeMeaning
\uCurrent username
\hHostname (short)
\wCurrent working directory (full path)
\WCurrent working directory (basename)
\tCurrent time (24h)

Example:

export PS1="[\u@\h \W]\$ "
# Result: [jane@laptop docs]$

V. Aliases: Productivity Shortcuts

Aliases allow you to create short names for long, complex commands.

# Add these to your .zshrc or .bashrc
alias ls='ls --color=auto'
alias ll='ls -lah'
alias gs='git status'
alias gcm='git commit -m'

In the next chapter, we'll stop typing manual commands and learn how to package them into Shell Scripts.