POSIX sh is the portability baseline; Bash adds arrays, [[ ]], and more. Containers and old systems may point /bin/sh to dash.
Shebang choices
#!/bin/sh # POSIX mode on many distros
#!/usr/bin/env bash # Bash features OKDebian/Ubuntu sh is often dash—Bash-only syntax breaks.
Portable test
# POSIX
if [ "$var" = "yes" ]; then echo ok; fi
# Bash
if [[ $var == yes ]]; then echo ok; fiQuote variables in [ ] to handle empty values safely.
Detect bash
if [ -n "$BASH_VERSION" ]; then
echo "running bash $BASH_VERSION"
fiFeature-detect when one script must run on both sh and bash.
Important interview questions and answers
- Q: Why [[ ]] not in sh?
A: It is a Bash keyword—not available in POSIX test. - Q: When use /bin/sh shebang?
A: Maximum portability for minimal install scripts and some embedded environments.
Self-check
- What breaks if you use [[ on dash sh?
- What variable indicates Bash?
Tip: If you need [[ ]], do not claim POSIX-sh portability—use Bash shebang.
Interview prep
- [[ in sh?
Not POSIX—Bash/ksh feature; dash sh breaks.
- $BASH_VERSION?
Indicates script runs under Bash.