Here's another version that works on Linux and macOS without using eval
and without having to delete backup files. It uses Bash arrays for storing the sed
parameters, which is cleaner than using eval
:
# Default case for Linux sed, just use "-i"sedi=(-i)case "$(uname)" in # For macOS, use two parameters Darwin*) sedi=(-i "")esac# Expand the parameters in the actual call to "sed"sed "${sedi[@]}" -e 's/foo/bar/' target.file
This does not create a backup file, neither a file with appended quotes.