← Back to Bash Course | Chapter 12: Error Handling & Debugging | Lesson 11 of 12

Portability

In this page:

  1. Portability

Portability

Scripts meant to run across different systems should avoid Bash-only features (like arrays or [[ ]]) if they need to work under plain POSIX sh, and should avoid assuming GNU-specific flags that differ on macOS/BSD tools. Testing on the actual target systems, and using #!/usr/bin/env bash instead of a hardcoded path, both improve portability. Being explicit about which shell and tool versions a script depends on prevents "works on my machine" surprises.

Note: If a script only uses POSIX features, give it a .sh extension and a #!/bin/sh shebang to signal it doesn't need Bash specifically.

Example: Portability

bash
#!/usr/bin/env bash
# Using env in the shebang finds bash wherever it's installed,
# which is more portable across different systems.
echo "Bash version: $BASH_VERSION"
echo "This script relies on Bash-specific features like arrays:"
arr=(1 2 3)
echo "${arr[@]}"

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.