Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference Between sh and Bash in Linux?
sh and bash are both command-line shells used in Unix-like operating systems, but they serve different purposes and have distinct capabilities. sh (Bourne shell) is the original POSIX-compliant shell, while bash (Bourne Again Shell) is an enhanced, feature-rich successor that extends sh's functionality.
What is sh?
The sh shell, originally developed by Stephen R. Bourne at Bell Labs, is the standard POSIX shell specification. It provides a minimal, portable command-line interface that works consistently across different Unix systems. sh focuses on compatibility and standardization, making it ideal for writing portable scripts.
#!/bin/sh echo "This is a POSIX-compliant shell script"
Key characteristics of sh include POSIX compliance, lightweight design, and universal availability on Unix-like systems. It lacks advanced features but ensures maximum portability.
What is bash?
The bash shell, developed by Brian Fox for the GNU Project, is an enhanced version of sh that adds numerous modern features while maintaining backward compatibility. It serves as the default shell on most Linux distributions and macOS.
#!/bin/bash echo "This is a bash script with enhanced features" history | tail -5
bash extends sh with features like command history, job control, tab completion, arithmetic evaluation, arrays, and advanced pattern matching. These enhancements make it more user-friendly and powerful for interactive use and complex scripting.
Key Differences
| Feature | sh | bash |
|---|---|---|
| Full Name | Bourne Shell | Bourne Again Shell |
| Developer | Stephen R. Bourne | Brian Fox |
| POSIX Compliance | Yes (strict) | Mostly (with extensions) |
| Command History | No | Yes |
| Job Control | No | Yes |
| Tab Completion | No | Yes |
| Arrays | No | Yes |
| Default Shell | No | Yes (most Linux distros) |
| Script Portability | High | Lower |
When to Use Each
Use sh when writing portable scripts that need to run on various Unix systems, embedded systems, or when strict POSIX compliance is required. Its minimal footprint makes it ideal for system scripts and environments with limited resources.
Use bash for interactive shells, complex scripts that benefit from advanced features, and when working primarily on Linux systems. Its enhanced functionality improves productivity for daily command-line tasks and sophisticated scripting projects.
Compatibility Considerations
While bash is largely backward-compatible with sh, scripts written specifically for bash may not work in a pure sh environment. To ensure maximum portability, use only POSIX-compliant features when writing sh scripts. Modern systems often link /bin/sh to bash or another POSIX-compliant shell like dash.
Conclusion
sh provides a standardized, portable foundation for shell scripting across Unix systems, while bash offers enhanced features and user-friendly improvements for modern Linux environments. Choose sh for maximum portability and bash for feature-rich interactive and scripting capabilities.
