Types of Shells

Linux and Unix systems support various types of shells, each with its own features and capabilities. Understanding the different shells helps you choose the right tool for your needs.

Common Shell Types

1. Bourne Shell (sh)

  • Original: The first Unix shell, developed by Stephen Bourne
  • Features: Basic scripting capabilities, simple syntax
  • Usage: Still used for system scripts and compatibility
  • Location: Usually /bin/sh
# Check if sh is available
$ which sh
/bin/sh

2. Bash (Bourne Again Shell)

  • Most Popular: Default shell on most Linux distributions
  • Features: Enhanced version of Bourne shell with many improvements
  • Advantages:
    • Command history
    • Tab completion
    • Job control
    • Arrays and associative arrays
    • Advanced scripting features
# Check Bash version
$ bash --version
GNU bash, version 5.1.16(1)-release

3. Zsh (Z Shell)

  • Advanced: Feature-rich shell with powerful customization
  • Features:
    • Excellent tab completion
    • Spelling correction
    • Plugin system
    • Themes support
  • Popular Framework: Oh My Zsh
# Install zsh (Ubuntu/Debian)
$ sudo apt install zsh

# Make zsh default shell
$ chsh -s $(which zsh)

4. Fish (Friendly Interactive Shell)

  • User-Friendly: Designed for ease of use
  • Features:
    • Syntax highlighting
    • Auto-suggestions
    • Web-based configuration
    • No configuration required
# Install fish (Ubuntu/Debian)
$ sudo apt install fish

# Start fish shell
$ fish

5. Dash (Debian Almquist Shell)

  • Lightweight: Minimal shell focused on speed
  • Usage: Often used as /bin/sh on Debian-based systems
  • Purpose: System scripts and boot processes

6. Tcsh (TENEX C Shell)

  • C-like Syntax: Similar to C programming language
  • Features: Command history, job control, aliases
  • Usage: Less common in modern systems

7. Ksh (Korn Shell)

  • Commercial: Developed by David Korn at Bell Labs
  • Features: Combines features of Bourne and C shells
  • Variants: ksh88, ksh93, mksh (MirBSD Korn Shell)

Checking Available Shells

View all available shells:

$ cat /etc/shells
/bin/sh
/bin/bash
/usr/bin/bash
/bin/rbash
/usr/bin/rbash
/bin/dash
/usr/bin/dash
/usr/bin/zsh
/usr/bin/fish

Check your current shell:

$ echo $SHELL
/bin/bash

# Or use ps command
$ ps -p $$
  PID TTY          TIME CMD
 1234 pts/0    00:00:00 bash

Check shell being used by a script:

$ ps -o pid,ppid,cmd

Shell Comparison

Feature sh bash zsh fish dash
Speed Fast Medium Medium Medium Very Fast
Scripting Basic Advanced Advanced Good Basic
Interactive Basic Good Excellent Excellent Basic
Compatibility High High Good Low High
Customization Limited Good Excellent Good Limited

Choosing the Right Shell

For Scripting:

  • bash: Best balance of features and compatibility
  • sh: Maximum portability across systems
  • dash: When speed is critical

For Interactive Use:

  • zsh: Power users who want customization
  • fish: Beginners who want user-friendly features
  • bash: Good default choice for most users

For System Administration:

  • bash: Most common, good documentation
  • sh: For system scripts that need portability

Switching Between Shells

Temporarily switch:

# Start a new shell session
$ zsh
$ fish
$ bash

# Exit back to previous shell
$ exit

Change default shell:

# Change default shell for current user
$ chsh -s /usr/bin/zsh

# Verify the change
$ grep $USER /etc/passwd

Shell Compatibility

When writing scripts, consider compatibility:

#!/bin/bash
# This script requires bash-specific features

#!/bin/sh
# This script should work with any POSIX shell

#!/usr/bin/env bash
# Portable way to find bash

Understanding the different types of shells available helps you make informed decisions about which shell to use for different tasks and environments.