๐ง Bash
Bash (Bourne Again Shell) is the most widely used generalโpurpose shell on Linux systems.
It combines POSIX sh compatibility with many powerful extensions, making it suitable for both scripting and interactive use.
๐ Who This Is For
- Developers writing internal tooling and automation
- DevOps/SRE engineers maintaining scripts on Linux systems
- Users migrating legacy bash scripts to more portable forms
- Anyone who needs to understand when bash is appropriate vs POSIX sh
๐งฉ Role in the Ecosystem
- Default interactive shell on many Linux distributions (though less so recently)
- Commonly assumed in tutorials, blog posts, and legacy scripts
- Often used as
/bin/bash explicitly in scripts that rely on extensions
- Not guaranteed to be available as
/bin/sh or on minimal/container images
๐งฉ Key Characteristics
- Superset of POSIX sh (but not strictly POSIX by default)
- Supports arrays, associative arrays,
[[ ... ]], process substitution, brace expansion
- Rich interactive features (history, completion, readline)
- Large ecosystem of scripts and tooling
๐ง Notable Features
| arr=(one two three)
echo "${arr[1]}"
|
| if [[ $var == foo* ]]; then
...
fi
|
โ ๏ธ Limitations & Pitfalls
- Not available by default on all systems (e.g., some minimal containers, embedded systems, older Unix)
- Scripts using bashโspecific features are not portable to
/bin/sh
- Some bashisms silently break when run under
dash, ash, or BusyBox sh
- Interactive configuration (
.bashrc) is not loaded in nonโinteractive scripts
๐ง When to Use Bash
- Internal tooling where you control the environment
- Developer machines with guaranteed
/bin/bash
- Scripts that benefit from arrays,
[[ ]], or process substitution
- Prototyping complex shell logic before rewriting in another language
โ
Best Practices
- Use
#!/usr/bin/env bash when you truly require bash
- Document that a script requires bash, not just โshโ
- Avoid bashisms in scripts intended for
/bin/sh or CI/CD runners
- Prefer POSIX syntax when portability matters
- Test scripts under
bash --posix if you aim for POSIX compatibility
๐งช Testing Bash Scripts
| bash -n script.sh # syntax check
bash -u script.sh # treat unset vars as errors
bash -x script.sh # trace execution
|
๐ง Summary
Bash is powerful and convenient, but not universal.
Use it deliberately where its features are needed, and fall back to POSIX sh when portability and minimalism are more important.