Terminal GuideTerminal Guide

sh

Bourne Shell / POSIX Shell

The original Unix shell, foundation for POSIX standard

POSIX CompliantReleased: 1979Official Site

Overview

The Bourne Shell (sh) is the original Unix shell, developed by Stephen Bourne at Bell Labs in 1979. Today, /bin/sh typically points to a POSIX-compliant shell like dash or bash. It serves as the standard for portable shell scripting.

Quick Facts

Full NameBourne Shell / POSIX Shell
CategoryPosix
POSIX CompliantYes
Config File.profile
Default OnPOSIX systems, Embedded systems
First Release1979

Who Should Use sh?

  • Script writers - Maximum portability across systems
  • System programmers - System initialization scripts
  • Embedded developers - Minimal resource environments
  • POSIX compliance needs - Standards-compliant scripting

Installation

sh is available on all Unix-like systems.

bash
# Check what sh points to
ls -la /bin/sh

# On Ubuntu/Debian, sh is usually dash
# On macOS, sh is bash (in POSIX mode)
# On FreeBSD, sh is ash

# Run a script with sh
sh script.sh

# Or use shebang
#!/bin/sh

Basic Usage

POSIX shell scripting basics.

bash
#!/bin/sh

# Variables
NAME="World"
echo "Hello, $NAME!"

# No arrays in POSIX sh!
# Use space-separated strings instead
fruits="apple banana cherry"
for fruit in $fruits; do
    echo "$fruit"
done

# Conditionals
if [ -f "file.txt" ]; then
    echo "File exists"
fi

# Functions
greet() {
    echo "Hello, $1!"
}
greet "User"

Configuration

sh configuration is minimal.

bash
# ~/.profile - Read by login shells

# Set PATH
PATH="$HOME/bin:$PATH"
export PATH

# Set environment variables
export EDITOR=vim

# Note: sh has limited interactive features
# For interactive use, consider bash or zsh

Key Features

POSIX Standard

The baseline for portable scripts

Universal Availability

Present on every Unix-like system

Minimal Dependencies

No external libraries required

Fast Startup

Lightweight with quick initialization

FAQ

Should I use sh or bash for scripts?

Use #!/bin/sh for maximum portability when your script uses only POSIX features. Use #!/bin/bash when you need bash-specific features like arrays.

What is the difference between sh and bash?

sh is the POSIX standard shell specification. bash is a specific implementation that includes many extensions beyond POSIX. bash can run in POSIX mode with --posix flag.

Summary

Key takeaways for sh:

  • Standard POSIX shell for maximum portability
  • Available on all Unix-like systems
  • Best choice for system scripts and cross-platform compatibility
  • Limited interactive features compared to modern shells

Official Documentation

For authoritative information, refer to the official documentation:

Written by Dai AokiPublished: 2026-01-20

Related Articles