Linux Mastery

The Human Knowledge Project


Chapter 13 — Shells & Environment Variables


Why This Chapter Matters

The Linux shell is far more than a place to type commands.

It is your primary interface to the operating system.

Through the shell you launch programs, manage files, customize your environment, automate repetitive tasks, and control nearly every aspect of Linux.

Understanding how the shell works—and how environment variables influence its behavior—is a major milestone in becoming an efficient Linux user.


Learning Objectives

Upon completing this chapter, you will be able to:


Introduction

Every command you have entered so far has passed through a program called the shell.

The shell accepts your commands, interprets them, and asks the Linux kernel to perform the requested work.

Although many new Linux users think they are communicating directly with the operating system, they are actually interacting with the shell.

Learning how the shell works opens the door to customization, scripting, automation, and efficient command-line workflows.


1. What Is a Shell?

A shell is a program that provides the interface between you and the Linux operating system.

It performs several important jobs:

Without a shell, Linux would be much more difficult to use interactively.

THKI Memory Aid


You
    ↓
Shell
    ↓
Kernel
    ↓
Hardware

The shell communicates with the Linux kernel on your behalf.

THKI Insight

Most users never communicate directly with the Linux kernel.

They communicate with a shell, which translates their commands into requests the kernel can execute.


2. Why Is It Called a Shell?

Think of the Linux kernel as the core of the operating system.

The shell surrounds that core, providing a convenient interface through which users interact with Linux.

Just as the shell of a nut protects the kernel inside, the Linux shell forms the outer layer through which commands pass before reaching the operating system.


3. Common Linux Shells

Linux supports several different command-line shells.

Each has its own strengths and style.

| Shell | Description |

|--------|-------------|

| bash | Bourne Again Shell |

| zsh | Z Shell |

| sh | Original Bourne Shell |

| fish | Friendly Interactive Shell |

| csh | C Shell |

Although they differ in features, they all perform the same fundamental job: accepting commands and communicating with the Linux kernel.


4. bash — Bourne Again Shell

bash is the most widely used Linux shell.

It serves as the default shell on many Linux distributions.

Popular features include:

To start another Bash shell:


bash

To display your current shell:


echo $SHELL

Example output:


/usr/bin/bash

5. zsh — Z Shell

zsh is another popular shell that emphasizes customization and interactive features.

Many users prefer it because it provides:

If installed, launch it with:


zsh

Comparing Bash and Z Shell

| Feature | bash | zsh |

|---------|--------|--------|

| Default on many Linux systems | Yes | Sometimes |

| Ease of learning | High | Moderate |

| Customization | Good | Extensive |

| Plugin ecosystem | Moderate | Large |

Both are excellent shells.

Many Linux professionals use Bash because it is nearly universal, while others prefer Z Shell for its customization features.


6. Understanding the Shell Prompt

The shell prompt is the text displayed before each command.

Example:


norm@BOX1:~$

A prompt commonly includes:

The prompt reminds you where you are before entering a command.

User Prompt vs. Root Prompt

Normal users typically see:


$

The root user normally sees:


#

The root prompt serves as an important reminder that commands entered at that prompt have administrative privileges.

THKI Insight

Before pressing Enter, glance at your prompt.

Accidentally running a command as the root user can have system-wide consequences.


7. What Are Environment Variables?

Environment variables are named values that store information used by:

They help Linux remember important information about your working environment.

Programs can read these variables to determine how they should behave.


8. Viewing Environment Variables

To display all environment variables, use either:


printenv

or:


env

Both commands display the current environment.

A typical variable appears as:


NAME=value

For example:


HOME=/home/norm

The name appears on the left.

Its value appears on the right.


9. Common Environment Variables

Several environment variables appear on nearly every Linux system.

| Variable | Purpose |

|----------|---------|

| HOME | Your home directory |

| USER | Your username |

| SHELL | Your current shell |

| PATH | Where Linux searches for commands |

| PWD | Present working directory |

| HOSTNAME | Computer name |

These variables help both Linux and your applications understand your working environment.

THKI Memory Aid


HOME
    ↓
Where I live
PATH
    ↓
Where Linux looks
USER
    ↓
Who I am
SHELL
    ↓
How I communicate

Students who remember these four variables already understand much of the Linux environment.


10. Displaying Variable Values

To display the value of a variable, use the echo command together with a dollar sign.

Example:


echo $HOME

Possible output:


/home/norm

Another example:


echo $USER

You can display any environment variable in the same way.


11. The Dollar Sign ($)

When placed before a variable name, the dollar sign tells the shell:

"Replace this variable with its stored value."

For example:


echo $PATH

does not display the word:


PATH

Instead, it displays the value stored inside the PATH variable.


12. The PATH Variable

PATH is one of the most important variables in Linux.

It tells the shell where to search for executable programs.

View it with:


echo $PATH

Example output:


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

Notice that each directory is separated by a colon:


:

Linux searches these directories from left to right until it finds the requested command.


13. Why PATH Matters

Suppose you type:


ls

You do not type:


/usr/bin/ls

Instead, the shell searches each directory listed in PATH.

When it finds:


/usr/bin/ls

it runs the program automatically.

Without PATH, you would need to type complete file paths for nearly every command.

THKI Insight

PATH is simply a list of places where Linux looks for programs.

When a command cannot be found, checking PATH is often one of the first troubleshooting steps.


14. Modifying PATH Temporarily

You can add another directory to your search path.

Example:


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

This appends:


/home/norm/scripts

to the existing PATH.

Programs inside that directory can now be run without typing their full path.

This change lasts only for the current shell session.


15. Temporary vs. Permanent Changes

Changes made directly at the command line disappear when the shell closes.

To make permanent changes, edit one of the shell's startup files.

The next time the shell starts, it reloads those settings automatically.

This allows Linux users to customize their working environment permanently.


16. Shell Configuration Files

Shell configuration files store personal preferences and startup commands.

They commonly contain:

For Bash, the most common files are:

| File | Purpose |

|------|---------|

| .bashrc | Interactive shell settings |

| .profile | Login shell settings |

| .bash_logout | Commands run when logging out |

These files are stored in your home directory.


17. Hidden Files

Files beginning with a period (.) are hidden by default.

Examples include:


.bashrc
.profile
.bash_logout

To display hidden files:


ls -a

Many important Linux configuration files are hidden in this way.


18. The .bashrc File

The .bashrc file is one of the most frequently customized files in Linux.

It often contains:

View it with:


less ~/.bashrc

The tilde (~) represents your home directory.

To edit it:


nano ~/.bashrc

or:


xed ~/.bashrc

After making changes, reload the file without logging out:


source ~/.bashrc

or:


. ~/.bashrc

19. Aliases

Aliases create shortcuts for longer commands.

For example:


alias ll='ls -l'

Now typing:


ll

is equivalent to typing:


ls -l

Aliases can save time, reduce typing, and simplify frequently used commands.

Many experienced Linux users maintain a large collection of custom aliases.


20. Exporting Variables

Variables created in the shell normally exist only within that shell.

To make a variable available to programs launched from the shell, export it.

Example:


export EDITOR=nano

Programs started from this shell can now access the variable:


EDITOR

Exporting variables allows programs to share information with one another.


21. Child Processes

When the shell starts another program, that program becomes a child process.

Child processes inherit exported environment variables from their parent shell.

This inheritance allows applications to use the same environment settings without requiring each program to define them separately.

THKI Insight

Think of exported variables as instructions a parent shell gives to every child program it starts.


22. Command History

Bash automatically remembers previously entered commands.

Display your command history with:


history

This is especially useful when repeating commands or reviewing earlier work.


23. Reverse History Search

One of Bash's most useful features is interactive history search.

Press:


Ctrl+R

Begin typing part of a previous command.

Bash immediately searches your command history for matching entries.

Many experienced Linux users rely on this feature dozens of times each day.


24. Tab Completion

Pressing the Tab key allows Bash to complete many commands and filenames automatically.

For example, if a directory named:


Documents

exists, typing:


cd Doc

then pressing:


Tab

may automatically complete:


cd Documents

Tab completion:

THKI Insight

Experienced Linux users rarely type complete filenames.

They let the shell complete them automatically.


25. Why Shells Matter

The shell is one of the defining characteristics of Linux.

Advanced Linux users often spend much of their time:

Learning the shell is not simply learning another program.

It is learning how to communicate efficiently with Linux itself.


26. Real-World Workflow

A typical Linux user might begin the day by:

The shell becomes the control center for daily work.


27. Safety Note

Shell configuration files influence every terminal session.

Before making major changes:

If an error occurs, restoring the original configuration is usually straightforward.


Chapter Summary

| Concept | Purpose |

|---------|---------|

| Shell | Command interpreter |

| Bash | Most common Linux shell |

| Z Shell | Highly customizable shell |

| Environment Variable | Stores configuration information |

| PATH | Directories searched for commands |

| .bashrc | Bash startup configuration |

| Alias | Command shortcut |

| export | Share variables with child processes |

| history | Display previous commands |

| Ctrl+R | Search command history |

| Tab Completion | Complete commands and filenames automatically |


Key Ideas

The Linux shell provides the interface between the user and the operating system.

Understanding:

makes Linux significantly more efficient and enjoyable to use.

Customizing the shell is one of the first steps toward making Linux your own.


Practice Exercises

  1. Display your current shell using:
  2. 
    echo $SHELL
    
  3. Display the values of:

using:


echo
  1. View all environment variables using:
  2. 
    printenv
    
  3. Display hidden files in your home directory.
  4. View your .bashrc file using:
  5. 
    less ~/.bashrc
    
  6. Create an alias for:
  7. 
    ls -lah
    
  8. Create your own environment variable.
  9. Export the variable.
  10. Start another Bash shell.
  11. Verify that the exported variable is available in the child shell.
  12. Display your command history.
  13. Use:
  14. 
    Ctrl+R
    

to locate a previously entered command.

  1. Practice tab completion with both commands and filenames.
  2. Add a temporary directory to your PATH.
  3. Explain the difference between a normal shell variable and an exported environment variable.
  4. Explain why aliases improve productivity.
  5. Describe the relationship between:

using your own words.


Looking Ahead

Now that you understand the Linux shell and how it manages your working environment, you are ready to begin automating repetitive tasks.

In the next chapter, we will introduce shell scripting, where individual commands become reusable programs capable of performing powerful automated workflows.