Practical Professional Linux — Foundation

Chapter 4 · Skill Level: Foundation

User Environment & Shell Basics

Configure the shell, environment variables, and startup files for an efficient, reproducible workflow.

A well-configured shell environment is a force multiplier. This chapter covers environment variables, PATH, aliases, startup files, history, and quoting — the settings that make day-to-day work fast and repeatable across sessions.

By the end of this chapter you will be able to

  • Inspect and set environment variables and understand PATH resolution.
  • Define aliases and shell functions for common tasks.
  • Configure startup files (.bashrc vs .bash_profile) correctly.
  • Use history effectively, including reverse search.
  • Apply quoting and escaping rules to control word-splitting and expansion.

4.1 Environment Variables

An environment variable is a named value the shell remembers. $PATH lists where the shell looks for programs; $HOME is your home folder; $USER is your name.

Reading and setting variables
echo $HOME            # your home directory
echo $PATH            # where commands are found
MYNAME=Josephine      # set a variable (no spaces around =)
echo "Hi, $MYNAME"    # use it
export MYNAME         # make it available to programs you launch

4.2 PATH Resolution

When you type ls, the shell searches each folder in $PATH, in order, until it finds a matching program. That’s why some commands ‘aren’t found’ — their folder isn’t on the PATH.

4.3 Aliases and Functions

An alias turns a long command into a short one. A function does the same but can take arguments.

Aliases and a handy function
alias ll='ls -lah'          # long, all, human-readable
alias gs='git status'
 
# A function that makes a folder and enters it
mkcd(){ mkdir -p "$1" && cd "$1"; }

4.4 Startup Files

Aliases set at the prompt vanish when you close the terminal. To keep them, add them to a startup file that runs each time a shell opens.

File When it runs
~/.bashrc Every new interactive (non-login) shell — most terminals. Put aliases here.
~/.bash_profile Login shells (e.g. SSH). Often just sources .bashrc.
/etc/profile, /etc/bash.bashrc System-wide, for all users (needs admin).
Persisting settings
# Add an alias permanently, then reload
echo "alias ll='ls -lah'" >> ~/.bashrc
source ~/.bashrc

4.5 Command History

Action How
Repeat last command Press Up, or type !!
Search history Press Ctrl+R, then type part of the command
See recent history history | tail -20
Run command number 42 !42

4.6 Quoting and Escaping

Quotes control how the shell reads your text. Double quotes keep variables working but protect spaces; single quotes take everything literally.

Double vs single quotes
name='Ada Lovelace'
echo "Hello, $name"     # Hello, Ada Lovelace  (variable expands)
echo 'Hello, $name'     # Hello, $name        (literal)
touch "my file.txt"      # quotes keep the space as one name

4.7 Guided Lab: Configure Your Environment

Estimated time: 20 minutes. Set up aliases, a function, and a PATH entry, make them permanent, and prove they survive a new shell.

  • Inspect your environment: echo $HOME, echo $USER, echo $PATH.
  • Create a personal bin folder: mkdir -p ~/bin.
  • Add it to PATH permanently: append export PATH="$HOME/bin:$PATH" to ~/.bashrc.
  • Add two aliases to ~/.bashrc: ll='ls -lah' and ..='cd ..'.
  • Add the mkcd function shown earlier to ~/.bashrc.
  • Apply changes with source ~/.bashrc, then test: ll, then mkcd testdir.
  • Open a brand-new terminal and confirm ll still works (it persisted).

Troubleshooting

Symptom Likely cause and fix
Alias works now but is gone after reopening the terminal You set it at the prompt only. Add it to ~/.bashrc and run source ~/.bashrc.
‘command not found’ for a script you wrote Its folder isn’t on PATH, or it isn’t executable. Add the folder to PATH and chmod +x the script.
Edited .bashrc but nothing changed Startup files only run for new shells. Run source ~/.bashrc, or open a new terminal.
A filename with spaces breaks a command Quote the variable: use “$file”. Unquoted spaces split it into multiple arguments.

Practice & Prove It

Write-the-command drills

  • Print the value of your PATH variable.
  • Set a variable GREETING to ‘hello world’ and print it with echo (mind the quotes).
  • Create a permanent alias gl for git log --oneline.
  • Show the last 15 commands from your history.
  • Add ~/scripts to the front of your PATH for the current session.

Quick quiz

  • Which file holds aliases for normal interactive terminals?
  • What does $PATH contain?
  • What is the difference between single and double quotes?
  • Which keyboard shortcut searches command history?
  • Why must there be no spaces around = in an assignment?

Key Takeaways

  • Environment variables store named values; PATH determines where commands are found.
  • Aliases and functions turn long, repeated commands into short ones.
  • Put permanent settings in ~/.bashrc and apply them with source ~/.bashrc.
  • History plus Ctrl+R lets you find and reuse past commands instantly.
  • Quote variables (“$var”) to handle spaces and special characters safely.

Next — Chapter 5: user and group management, the foundation of multi-user security.