Supporting cross-platform sed

Updated April 7, 2024
Created October 20, 2022

When it comes to scripting for both Mac and Linux, one popular choice is to use Perl for its simplicity in writing one-liners. For example, the following command can be used to replace all instances of "original" with "replacement" in a file:

perl -i -pe s|original|replacement| <file>

However, when it comes to cross-platform scripting, it's important to consider compatibility between different systems. One way to ensure compatibility is to use sed command, which is available on both Mac and Linux by default.

Here's an example of a script that can be used to replace all instances of "foo" with "bar" in a file named "file.txt":


# Define the file and the search and replace strings

# Check if the file exists
if [ ! -f "$file" ]; then
  echo "Error: $file does not exist"
  exit 1

# Backup the original file
cp "$file" "$file.bak"

# Check if the '-i' option is supported
if sed --version 2>/dev/null | grep -q GNU; then
  # GNU sed
  sed -i "s/$search/$replace/g" "$file"
  # BSD sed
  sed -i "" "s/$search/$replace/g" "$file"

echo "All instances of '$search' have been replaced with '$replace' in $file"