The short answer

#

Use sed with the -i flag to edit a file in place. The catch is that GNU sed (Linux) and BSD sed (macOS) disagree on how -i works. On macOS you must pass a backup suffix, even an empty one. Attaching a suffix directly to the flag works on both.


_10
# Linux (GNU sed)
_10
sed -i 's/old/new/g' file.txt
_10
_10
# macOS (BSD sed)
_10
sed -i '' 's/old/new/g' file.txt
_10
_10
# Works on both, and keeps a backup as file.txt.bak
_10
sed -i.bak 's/old/new/g' file.txt

Why it happens

#

sed is a stream editor. By default it reads a file, applies your script and prints the result to standard output, leaving the original untouched. Editing in place is not part of the POSIX standard, so each implementation added -i in its own way:

  • GNU sed treats the suffix as optional, and it must be attached to the flag, as in -i.bak. A bare -i means "no backup".
  • BSD sed, which macOS ships, requires a suffix as the next argument. An empty string, '', means "no backup".

So the Linux command breaks on macOS, and the macOS command breaks on Linux. On macOS, sed -i 's/old/new/g' file.txt takes your script as the backup suffix and then tries to parse the file name as a script, which gives an error such as invalid command code. On Linux, sed -i '' 's/old/new/g' file.txt treats the empty string as the script and your real script as a file name, so it reports that it cannot read s/old/new/g.

How to fix it

#

Attach a backup suffix

The most portable form is a suffix glued to the flag. Both versions accept it. Delete the backups once you have checked the result:


_10
sed -i.bak 's/http:/https:/g' config.yml
_10
diff config.yml.bak config.yml
_10
rm config.yml.bak

Replace across many files

Let find pick the files and run sed on them. Ending -exec with + passes many files to each sed call, and file names with spaces are handled safely:


_10
find src -type f -name '*.js' -exec sed -i.bak 's/oldName/newName/g' {} +
_10
find src -type f -name '*.js.bak' -delete

To only touch files that contain the text, list them with grep first:


_10
grep -rl --exclude-dir=.git 'oldName' src

Use a different delimiter for paths

The character after s is the delimiter, so you do not have to escape every slash in a path:


_10
sed -i.bak 's|/usr/local/bin|/opt/homebrew/bin|g' setup.sh

Use the same sed everywhere

If you write scripts that run on both macOS and Linux, install GNU sed on your Mac with Homebrew. It is installed as gsed so it does not replace the system version:


_10
brew install gnu-sed
_10
gsed -i 's/old/new/g' file.txt

Alternatively, Perl's in-place mode behaves the same on both systems:


_10
perl -pi -e 's/old/new/g' file.txt

Common pitfalls

#
  • Forgetting the g flag. Without it, sed replaces only the first match on each line.
  • Not escaping special characters. In the pattern, ., *, [ and the delimiter have special meaning. In the replacement, & inserts the whole match, so write \& for a literal ampersand.
  • Running a replacement over .git or node_modules. Narrow the search with find options or --exclude-dir.
  • Using single quotes when you need a variable. Use double quotes, as in "s/old/$new/g", and remember that a slash inside the variable will break the command unless you change the delimiter.
  • Expecting \b word boundaries to work everywhere. GNU sed supports them; on macOS use [[:<:]] and [[:>:]] instead, or use Perl.
  • Skipping a dry run. Run the command without -i first, or pipe it through diff, to see what would change.
#
file under:
Bash

quick question

Was this resource helpful?