Tip - Fix Sed I Issue On Mac Os

Small world. Big idea!

The Problem

When I working with sed to write a file script for work. I get an error when I use sed -i to replace a word in a script file but I get an error.

I ran the command:

sed -i 's/pattern/replacement/g' file.sh
# or
sed -i 's/pattern/$replacement/g' file.sh # $replacement is from the environment

An Error occurred:

sed: 1: "file.sh": unterminated substitute in regular expression

This sed version was May 10, 2005 as I saw in man sed.

Fix It


In sed there are 2 options that can easily be confused with -i and -e options.

Download the newest version of GNU sed from here:

π ~/opt/ ❯ tar xfz sed-4.9.tar.gz && cd sed-4.9
π ~/opt/ ❯ ./configure
π ~/opt/ ❯ make
π ~/opt/ ❯ sudo make install