Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 8

Understanding Shell Types

Recommended resources

Hello! Welcome to your final lesson in the "Linux Fundamentals and System Startup" module.

In our previous lesson, we explored how to use Linux's built-in documentation systems like man and info. We learned that some commands, like cd, are "shell built-ins" and have their own help system. This naturally leads to today's topic: the shell itself.

This lesson directly addresses the learning outcome: Distinguish between different shell types (e.g., bash, sh, zsh). We will define what a shell is, explore the history and features of the most common shells you'll encounter, and understand why the choice of shell is important for both interactive use and scripting. This knowledge is foundational for nearly everything else you will do on the command line and is a key area for the CompTIA Linux+ exam.

1. What is a Linux Shell?

Before we compare different shells, let's establish what a shell is. You've been using it every time you open the terminal, but it's important to have a precise definition. In short, the shell is a special program that acts as a command-line interpreter. It takes the commands you type, interprets them, and asks the operating system's core, the kernel, to execute them.

What are the Different Types of Shells in Linux?

For a clear and concise explanation of a shell's role, please read the introductory section of the article 'What are the Different Types of Shells in Linux?' from DigitalOcean.

Read the section titled 'What is a Linux Shell'. This will solidify your understanding of the shell as the crucial interface between you and the kernel.

Think of the shell as your translator. You speak to it in the language of commands and options, and it translates your requests into a language the kernel understands.


2. The Main Shells: A Family Tree

While many shells exist, you will most frequently encounter three main types in the Linux world: sh, bash, and zsh. Understanding their relationship and differences is key.

sh - The Bourne Shell

The Bourne Shell (sh) is the original UNIX shell, developed in the 1970s. It is simple, fast, and, most importantly, it forms the basis of a standard called POSIX (Portable Operating System Interface). A script written to the POSIX standard should, in theory, run on any compliant system (Linux, macOS, BSD, etc.). For this reason, sh is the gold standard for writing highly portable scripts.

bash - The Bourne-Again Shell

As its name cleverly implies, the Bourne-Again Shell (bash) was created as a free, open-source replacement and enhancement for the original Bourne Shell. It is a superset of sh, meaning it supports everything sh does and adds many more features:

  • Command history accessible with the arrow keys.
  • More advanced scripting features like arrays and complex loops.
  • Command-line editing.

bash is the default login shell on the vast majority of Linux distributions, including the Ubuntu VM you set up. It offers a great balance between powerful features and sh compatibility.

zsh - The Z Shell

The Z Shell (zsh) is a more modern shell that is also a superset of sh. It is highly compatible with bash but includes even more advanced features, particularly for interactive use:

  • Smarter, more powerful tab-completion.
  • Spelling correction for commands.
  • Advanced theming and a massive ecosystem of plugins (popularly managed by frameworks like "Oh My Zsh").

zsh has become the default shell on macOS and is a favorite among many developers for its productivity-boosting features.

What's the Difference Between Bash, SH, and ZSH?

The article 'What's the Difference Between Bash, SH, and ZSH?' provides excellent analogies and practical advice for understanding these three shells.

Read the sections 'The Core Differences Between sh, bash, and zsh' and 'When to Use Each Shell'. This will give you a clear framework for thinking about their distinct roles.


3. POSIX Compliance and Scripting Differences

The most significant distinction for scripting is POSIX compliance. A script starting with #!/bin/sh signals that it should be run by a POSIX-compliant shell. While bash and zsh have features beyond the POSIX standard, they can run sh scripts.

However, a different type of shell, like fish (the Friendly Interactive Shell), is intentionally not POSIX-compliant. It uses a different syntax for basic operations like setting variables. This makes it very user-friendly for interactive use but means bash scripts won't run correctly with it, and its own scripts are not portable.

Bash vs ZSH vs Fish: What's the Difference?

To see a practical demonstration of what 'not POSIX compliant' means, and to get a great comparison of Bash, Zsh, and Fish, let's watch a video from Eric Murphy.

Watch from 05:31 to 10:00. The presenter first highlights the user-friendly, out-of-the-box features of Fish. Then, he demonstrates how setting a variable in Fish is different from the POSIX way, which works in Bash and Zsh. This is a perfect illustration of why POSIX compliance is so important for writing scripts that can be shared and run on different systems.

Test your understanding!

You find an installation script online for a program you want to use. The first line of the script is #!/bin/sh. What does this tell you about the script, and why is it a good sign?

Show answer

The line #!/bin/sh (called a "shebang") tells the system to execute the script using the Bourne shell (sh). Because sh adheres to the POSIX standard, this is a good sign that the script is written to be highly portable and should run correctly on almost any Linux or Unix-like system, regardless of the user's default interactive shell.

Here is a visual summary of the features across various shells. You can see how bash and zsh build upon the foundation of sh.

Linux Shell Comparison Chart
This table provides a feature-by-feature comparison of common Linux shells. Notice how features like 'Auto suggestions' and 'Syntax highlighting' are native to `fish` and `zsh` but often require extra configuration in `bash`.

4. Practical Shell Management

Now, let's get hands-on in your Ubuntu VM and learn how to manage shells.

Identifying Your Shells

First, how do you know what shell you are using? There are a few key commands.

The Confusion With Linux Shells

The video 'The Confusion With Linux Shells' by DistroTube clearly demonstrates how to find out which shells are available, which shell is your default, and which one you are currently running.

First, watch from 01:10 to 03:00 to learn how to list available shells (chsh -l), check your default shell (echo $SHELL), and see user shells in /etc/passwd. Then, watch from 07:38 to 09:15 to understand the difference between your default shell and the one you're currently in, using echo $0 and ps -p $$.

Let's try these in your terminal:

  1. List available shells on your system:
    cat /etc/shells
    
  2. See your user's default login shell:
    echo $SHELL
    
    This shows the shell that starts when you log in. For your Ubuntu VM, it should be /bin/bash.
  3. See the currently running shell process:
    echo $0
    
    This usually shows the name of the current shell. It might say bash or -bash.

The sh "Illusion"

A common point of confusion is sh. As the video mentioned, sh is not usually a separate program on modern Linux systems. It's a symbolic link to bash. You can verify this yourself.

ls -l /bin/sh

You will likely see output like /bin/sh -> bash. This means when a script asks for sh, the system actually runs bash in a special POSIX compatibility mode.

Changing Your Shell

You can change your default shell to any listed in /etc/shells. For example, let's install zsh and see how to switch to it.

  1. Install zsh:
    sudo apt update
    sudo apt install zsh -y
    
  2. Change your shell using chsh (change shell):
    chsh -s /bin/zsh
    
    You will be prompted for your password.

The change only takes effect when you start a new login session. You must log out and log back in. When you open a terminal after logging back in, zsh may greet you with a first-time configuration wizard. You can press q to quit and use the default settings for now.

You can always switch back to bash by running chsh -s /bin/bash and logging out/in again.

A Critical Warning: While it's safe to change your user's shell, you should never change the root user's shell. System scripts rely on /bin/bash being the root shell, and changing it can lead to a broken system that may not even boot.

Test your understanding!

You've changed your default shell to zsh. You open a terminal and run echo $SHELL, and the output is /bin/zsh. Then, you type bash and press Enter. Now, if you run echo $0, what output would you expect, and why?

Show answer

You would expect the output to be bash. Although your default login shell is zsh, by typing bash, you have started a new bash process inside your zsh session. The echo $0 command shows the name of the currently running process, which is now bash. You can type exit to close the bash shell and return to your zsh shell.


Conclusion

You now have a solid understanding of the most common Linux shells and the practical skills to manage them. This is a vital step in your journey, as the shell is the environment where you'll spend most of your time as a Linux administrator.

Key Takeaways:

  • A shell is a command-line interpreter that communicates between you and the Linux kernel.
  • sh (Bourne Shell) is the original, POSIX-standard shell, valued for writing portable scripts.
  • bash (Bourne-Again Shell) is the default on most Linux systems, offering a rich feature set while maintaining compatibility with sh.
  • zsh (Z Shell) is a modern alternative with powerful interactive features like advanced autocompletion and theming.
  • You can identify your current shell with echo $0, your default with echo $SHELL, and change it with chsh -s.
  • Never change the root user's default shell.

This lesson concludes the first module, "Linux Fundamentals and System Startup." You've covered a lot of ground, from the system architecture and boot process to finding help and understanding the shell environment.

In our next lesson, we will begin Module 2: Command Line and File System Navigation. We'll put our shell knowledge to immediate use by learning the basic commands to view system and user information.