Last modified: October 10, 2024

This article is written in: 🇺🇸

Shells

A Unix shell is a command-line interpreter that provides a user interface for accessing an operating system's services. It allows users to execute commands, run programs, and manage system resources. The shell acts as an intermediary between the user and the operating system kernel, translating user commands into actions performed by the system.

The Interaction Model

The interaction between the user, shell, and operating system can be visualized as follows:

+-------------------+       +----------------+       +--------------------+
|                   |       |                |       |                    |
|    User Input     |<----->|     Shell      |<----->|  Operating System  |
| (Keyboard/Screen) |       | (e.g., Bash)   |       |    (Kernel/HW)     |
|                   |       |                |       |                    |
+-------------------+       +----------------+       +--------------------+

Common Shells

There are several types of shells available, each with unique features:

Shell Description Benefits Considerations/Drawbacks
bash (Bourne-Again SHell) The default shell on most Linux distributions; backward-compatible with the original Bourne shell. Widely used, with extensive scripting support and community resources. Lacks some advanced features present in newer shells like zsh.
zsh (Z Shell) Known for its rich feature set, including improved auto-completion, spell correction, and theming capabilities. Highly customizable, with better autocompletion and plugins. Slight learning curve for users unfamiliar with its configuration.
ksh (Korn SHell) Combines features of the Bourne shell and the C shell (csh). Useful for scripting, combining the best of both worlds (Bourne and C shell). Not as widely adopted as bash or zsh.
tcsh (TENEX C Shell) An enhanced version of the C shell, featuring command-line editing and programmable word completion. Better user experience with command-line editing features. Less common compared to bash or zsh.
sh (Bourne SHell) The original Unix shell, simple and portable. Lightweight and portable for basic scripting tasks. Lacks many modern features available in newer shells.

Examining Available Shells

To see which shells are installed on your system, inspect the /etc/shells file. This file lists all the valid login shells available.

cat /etc/shells

Example Output:

/bin/sh
/bin/bash
/bin/dash
/bin/zsh
/usr/bin/zsh

Identifying Your Current Shell

To determine your current active shell, you can use several methods:

Method 1: Using the $SHELL Variable

echo "$SHELL"

Note: The $SHELL variable shows your default login shell, not necessarily the shell you're currently using.

Method 2: Inspecting the Shell Process

ps -p "$$" -o comm=

Method 3: Using echo "$0"

echo "$0"

Switching Shells

Temporarily Switching Shells

You can start a different shell session by typing its name:

zsh

To return to your previous shell, type exit or press Ctrl+D.

Permanently Changing Your Default Shell

To change your default login shell, use the chsh (change shell) command:

chsh -s /bin/zsh

Important: The shell must be listed in /etc/shells; otherwise, chsh will not accept it.

Bash Configuration Files

When Bash starts, it reads and executes commands from various startup files. These files allow you to customize your shell environment.

Types of Shells

Understanding which configuration files are read depends on how the shell is invoked:

Configuration Files Overview

I. Global Configuration Files (affect all users):

II. User-Specific Configuration Files (affect only the current user):

Bash Startup Sequence

For Login Shells:

  1. Bash reads /etc/profile.
  2. Then it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile (in that order) and reads the first one it finds.

For Interactive Non-Login Shells:

  1. Bash reads /etc/bash.bashrc or /etc/bashrc (system-wide configuration).
  2. Then it reads ~/.bashrc (user-specific configuration).

Best Practice: Source ~/.bashrc from ~/.bash_profile

To ensure that your settings are consistent across all shell types, it's common to source ~/.bashrc from ~/.bash_profile.

Example ~/.bash_profile:

# ~/.bash_profile

# Source the user's bashrc if it exists
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

Sample ~/.bashrc File

# ~/.bashrc

# Source global definitions if any
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# Alias definitions
alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

# Environment variables
export EDITOR='nano'
export HISTSIZE=1000
export HISTFILESIZE=2000

# Prompt customization
PS1='\u@\h:\w\$ '

# Functions
extract() {
    if [ -f "$1" ]; then
        case "$1" in
            *.tar.bz2)   tar xjf "$1"   ;;
            *.tar.gz)    tar xzf "$1"   ;;
            *.bz2)       bunzip2 "$1"   ;;
            *.rar)       unrar x "$1"   ;;
            *.gz)        gunzip "$1"    ;;
            *.tar)       tar xf "$1"    ;;
            *.tbz2)      tar xjf "$1"   ;;
            *.tgz)       tar xzf "$1"   ;;
            *.zip)       unzip "$1"     ;;
            *.Z)         uncompress "$1";;
            *.7z)        7z x "$1"      ;;
            *)           echo "Don't know how to extract '$1'..." ;;
        esac
    else
        echo "'$1' is not a valid file!"
    fi
}

Terminals

A terminal emulator is a program that emulates a physical terminal within a graphical interface, allowing users to interact with the shell.

Terminal Emulator Features

Common Terminal Emulators

Terminal Emulator Description Benefits Considerations/Drawbacks
GNOME Terminal Default terminal emulator on GNOME desktop environments. Integrated with GNOME, easy to use. Lacks some advanced customization features.
Konsole Default terminal emulator on KDE Plasma desktop environments. Highly customizable and integrates well with KDE. Primarily designed for KDE, may not be ideal for other environments.
xterm Basic terminal emulator for the X Window System. Lightweight and highly portable. Lacks modern features like tabs or split views.
Terminator Allows arranging multiple terminals in grids. Ideal for multitasking with a grid layout. May be overkill for basic terminal usage.
iTerm2 Popular terminal emulator for macOS with advanced features. Offers split panes, hotkeys, and extensive customization. Only available on macOS.

Opening a Terminal

Terminal Shortcut

Challenges

  1. Find if there are any existing aliases for a command, like cat. Use alias cat to see the aliases for cat.
  2. Display all aliases currently defined in your shell. Simply execute alias without any arguments.
  3. Open ~/.bashrc in a text editor, add a new alias like alias ll='ls -la'. Save the file, reopen your terminal, and verify the new alias. To remove it, delete or comment out the line in ~/.bashrc, then save and restart your terminal.
  4. Use the find command to search your system for files containing 'profile' in their name. Try find / -name '*profile*'.
  5. Create a new user whose default shell is a non-standard program. For example, useradd -s /bin/tar username creates a user with /bin/tar as their shell. Be aware of the implications this may have on user interaction with the system.
  6. Change your default shell using chsh -s /path/to/shell, then open a new terminal session and explore the new environment. Experiment with commands like alias, set, and declare -f to inspect custom variables, aliases, and functions.

Table of Contents

    Shells
    1. The Interaction Model
    2. Common Shells
    3. Examining Available Shells
    4. Identifying Your Current Shell
      1. Method 1: Using the $SHELL Variable
      2. Method 2: Inspecting the Shell Process
      3. Method 3: Using echo "$0"
    5. Switching Shells
      1. Temporarily Switching Shells
      2. Permanently Changing Your Default Shell
    6. Bash Configuration Files
      1. Types of Shells
      2. Configuration Files Overview
    7. Bash Startup Sequence
      1. For Login Shells:
      2. For Interactive Non-Login Shells:
    8. Best Practice: Source ~/.bashrc from ~/.bash_profile
    9. Sample ~/.bashrc File
    10. Terminals
      1. Terminal Emulator Features
      2. Common Terminal Emulators
      3. Opening a Terminal
    11. Challenges