sh
Bourne Shell / POSIX Shell
The original Unix shell, foundation for POSIX standard
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 Name | Bourne Shell / POSIX Shell |
| Category | Posix |
| POSIX Compliant | Yes |
| Config File | .profile |
| Default On | POSIX systems, Embedded systems |
| First Release | 1979 |
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.
# 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/shBasic Usage
POSIX shell scripting basics.
#!/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.
# ~/.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 zshKey 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:
Related Articles
Bash - GNU Bourne-Again Shell
The most widely used shell on Linux systems with powerful scripting capabilities
Dash - Debian Almquist Shell
Fast and lightweight POSIX-compliant shell for system scripts
Ksh - Korn Shell
Enterprise Unix shell combining best features of sh and csh