Bash – 10 tips to improve your code quality and Linux/MacOS compatibility that you need to know

In this article we will provide you with some tips and best alternatives to ensure compatibility between Linux and macOS. Bash is a powerful shell scripting language available on both platforms, and these tips will help you write efficient and cross-platform scripts.

So let’s go for the tips!

  1. Use Shebangs: Always start your bash scripts with a shebang (#!) line to ensure they are executed with the correct shell on both Linux and macOS. The common path for bash is /bin/bash, so the shebang should look like:
#!/bin/bash
  1. Avoid Bashisms: Try to stick to POSIX-compliant shell scripting to ensure maximum compatibility. Avoid using Bash-specific features that may not be available in other shells. For example, prefer $(command) over `command` for command substitution.
  2. Check Commands’ Availability: Before running specific commands in your script, check if they are available on the target system. Use the command -v or type commands to verify command existence:
if ! command -v some_command > /dev/null 2>&1; then
  echo "some_command not found, please install it."
  exit 1
fi
  1. Portable Paths: Use dirname and $0 to create portable paths within your script. This ensures that your script can find other resources regardless of the current working directory:
script_dir="$(cd "$(dirname "$0")" && pwd)"
  1. Avoid Parsing ls: Parsing ls output is generally not recommended due to its unreliable and platform-specific nature. Instead, use shell globs for file matching:
# Bad
files=$(ls *.txt)

# Good
files=( *.txt )
  1. Use Array Instead of String Manipulation: When dealing with lists or collections of items, use arrays instead of string manipulation. Arrays are more robust and reliable:
# Bad
items="item1 item2 item3"
for item in $items; do
  echo "$item"
done

# Good
items=("item1" "item2" "item3")
for item in "${items[@]}"; do
  echo "$item"
done
  1. Quoting Variables: Always quote variables to avoid word splitting and globbing issues:
# Bad
file="my file.txt"
rm $file

# Good
file="my file.txt"
rm "$file"
  1. Use Shellcheck: Shellcheck is a powerful static analysis tool that helps you find and fix common issues in shell scripts. It can detect many compatibility and best-practice problems. Install and use it to improve your script quality:
# Install Shellcheck on Debian/Ubuntu
sudo apt-get install shellcheck

# Install Shellcheck on macOS (assuming you have Homebrew)
brew install shellcheck

# Run Shellcheck on your script
shellcheck your_script.sh
  1. Test on Both Systems: If possible, test your scripts on both Linux and macOS to ensure they work correctly on both platforms. Consider using virtual machines or containerization tools like Docker to set up a test environment for each platform.
  2. Avoid code repetition. Make use of shared functions and load the ones you need by sourcing them "source path/functionsX.sh". This way you keep your code clean and also organize better what each function does!

By following these tips, you can create bash scripts that are efficient, reliable, and compatible with both Linux and macOS. Happy scripting!

Deixe um comentĂ¡rio

O seu endereço de e-mail nĂ£o serĂ¡ publicado. Campos obrigatĂ³rios sĂ£o marcados com *