Shell Programming  «Prev  Next»
Lesson 3 Deciding which shell to program for
Objective Distinguish between the primary uses of each shell to decide which to use.

Choosing the Right Unix Shell for Your Work

Choosing the right Unix shell depends on your primary use case: Bash remains the best all-round choice for most users due to its universal availability, extensive documentation, and compatibility with virtually all scripts you'll encounter. If your work involves heavy interactive use, modern features like better autocomplete, syntax highlighting, and sane defaults make Zsh (with Oh My Zsh or similar frameworks) or the increasingly popular Fish far more productive and enjoyable for daily command-line work. For scripting intended to run on minimal or embedded systems, or when strict POSIX compliance matters, dash or ash offer significantly faster execution and lower memory footprint than Bash, which is why distributions like Ubuntu and Debian use dash as the default /bin/sh.

From “Which shell do I have?” to “Which shell should I use?”

Modern Unix and Linux systems provide several command-line shells. As a user, you experience one shell as your default login shell, but you are free to choose a different shell for specific tasks or scripts. The goal of this lesson is to help you distinguish the primary uses of each shell so you can make an informed choice.

A natural first question is:

Which shell should I use for interactive work, and which shell should I use for scripts?

Your system administrator or account provisioning process has already chosen a default login shell for you. On many Linux systems this is bash, on some Unix systems it might be ksh, and on macOS it is often zsh. You can always change or temporarily override that choice, but you should first know what you are currently using.


Linux Shell Scripting

Finding and changing your default shell

To see which shell is configured as your default login shell, use one of the following commands at the prompt:

echo "$SHELL"
ps -p $$ -o comm=

The first command shows the shell configured for your account (for example, /bin/bash or /bin/zsh). The second command shows the name of the process currently executing your commands.

If your course or workplace recommends a particular shell and your default does not match it, you can usually ask your system administrator to change it or use a command like chsh (change shell) where that is permitted. Even if your login shell remains csh or tcsh, you can still run a different shell for scripting by invoking it explicitly (for example, typing sh or bash at the prompt).


Shells for interactive work

Interactive shells are optimized for day-to-day command-line use. Popular examples include:

Most modern interactive shells provide a similar set of convenience features:

  1. Command history so you can re-run or edit previous commands.
  2. Aliases and shortcuts for frequently used commands or options.
  3. Tab completion for filenames, commands, and sometimes options.
  4. Built-in arithmetic for quick calculations at the prompt.
  5. Job control to manage multiple running commands in one terminal session.

For interactive work, you will typically choose the shell that feels most productive and comfortable to you. Many administrators recommend bash or zsh for this purpose because of their strong ecosystem and tooling.

Shells for programming and scripting

The second part of the decision is: Which shell should you program for? This question is more about portability and predictable behavior than personal taste.

Historically, the C shell (csh) and its enhanced cousin tcsh were popular for interactive use, but they have well-known limitations as scripting languages:

  1. Weak or awkward input and output redirection for complex scripts.
  2. No true functions (subroutines) in the traditional C shell model.
  3. Ambiguous or surprising syntax that can lead to subtle bugs.

Because of these issues, C-style shells are generally not recommended for new shell scripts. Many modern shells build on the original Bourne shell model and provide a more robust foundation for scripting.

Why this course focuses on Bourne-compatible shells

Most modern Unix and Linux systems include a Bourne-compatible shell such as:

Scripts written to the POSIX sh standard (a portable subset of shell features) have several advantages:

  1. Portability: A script that runs in /bin/sh is likely to run on a wide variety of Unix and Linux systems.
  2. Compatibility: Many shells can interpret POSIX sh scripts, even if they offer additional features.
  3. Predictability: By targeting a well-defined standard, you avoid relying on shell-specific extensions that may not exist everywhere.
  4. Realistic examples: Much existing system and administration code is written in a Bourne-compatible style, so you learn from real-world patterns.

For this course, we will primarily use the Bourne-compatible shell (referred to here as sh) for scripts. You may log in using another shell such as bash or csh, but your script files will use a shebang line to specify the interpreter explicitly, for example:

#!/bin/sh
# Your portable script goes here

By doing this, you keep your interactive environment flexible while your scripts remain stable and portable.

Practical decision rules

When you sit down to decide “Which shell should I use?” apply these simple rules:

As you progress through this module, you will see examples that highlight when a feature is specific to one shell and when it is available across several shells. This will help you refine your own standards for which shell to use in different situations.

Looking ahead

Shell scripts are everywhere on Unix systems: startup scripts, maintenance tasks, automation utilities, and more. By understanding the strengths and intended uses of each shell, you can choose a login shell that makes you productive while writing scripts that remain portable, maintainable, and easy to debug.

In the next lesson, you will explore how shell scripts are used across the system and begin practicing with scripts of your own.

Unix Shell – Quiz

Use the following quiz to check your understanding of shell types and their primary uses.
Unix Shell - Quiz

SEMrush Software 3 SEMrush Banner 3