Introduction
In the Linux world, the command line is a fundamental tool for administrators, developers, and advanced users. Although many associate the terminal with bash, there is a more minimal and standardized alternative known as sh. This command invokes the standard POSIX shell, whose behavior is defined by the POSIX.1‑2008 specification and guarantees portability across different Unix‑like systems. In this article we will explore what sh is, its main features, how to use it effectively, and in which situations it is preferable over other shells.
What is sh?
The sh command is not a specific shell, but an interface that points to the system’s POSIX shell implementation. In most Linux distributions, sh is a symbolic link to dash (Debian Almquist Shell) or to bash when executed in POSIX mode. Regardless of the underlying implementation, invoking sh ensures that the interpreter strictly follows the POSIX standard, avoiding extensions specific to bash or other shells.
Main Features
- POSIX compatibility: only allows syntax and utilities defined in the standard.
- Reduced resource consumption: ideal for startup scripts and embedded environments.
- Predictability: behavior is the same on any POSIX‑compliant system.
- Lack of advanced features: does not include associative arrays, complex parameter expansion, or interactive debugging.
Basic Usage
To execute a simple command with sh just type it in the terminal:
sh -c "echo Hello world"
The -c option tells sh to read the command from the following string. Another common way is to create a script file and give it execute permission:
#!/bin/sh
printf 'Kernel version: %s\\n' "$(uname -r)"
Save the content as version.sh, run chmod +x version.sh, and then ./version.sh.
Practical Examples
- Check if a directory exists and create it if not:
#!/bin/sh
[ -d "$HOME/backup" ] || mkdir -p "$HOME/backup"
#!/bin/sh
for f in *; do
[ -f "$f" ] && printf '%s: %s\\n' \"$f\" \"$(stat -c%s \"$f\")\"
done
#!/bin/sh
cat /etc/passwd | grep '^/home' | cut -d: -f1
Differences with bash
- Bash includes features such as arrays,
[[ ]]for advanced tests and$()with unlimited nesting. - In POSIX mode (
bash --posix) bash approachessh, but still allows certain extensions if not strictly enabled. - Scripts that use
share more portable; those that depend on bash may fail on minimalist systems like Alpine Linux or heavily reduced container environments.
When to use sh
It is recommended to use sh in the following scenarios:
- System initialization scripts (
/etc/init.d,systemdservice files that callExecStart). - Container environments where a minimal image is desired (for example,
alpine). - When you need to guarantee that the script runs identically on any Unix‑like distribution.
- For simple administrative tasks where advanced bash features are not required.
Conclusion
The sh command embodies the essence of the standard POSIX shell: simplicity, portability, and predictability. Although it lacks many of the conveniences offered by bash, its value lies in providing a reliable environment for scripts that must run across diverse systems without surprises. Knowing when and how to use sh is an essential skill for any professional working with Linux who wishes to write robust and portable code.
This post is also available in ESPAÑOL.