Search code examples
pythonsedshebang

Adding shebang with mac


I want to add a shebang at the beginning of my file.py script using sed in Mac.

My file.py starts like this:

import os
import sys
import shutil

I have tried:

sed -i '.bak' '1 s|^.*$|#!/usr/bin/env python2|g' file.py

But then my file looks like this (the first line has been substituted by the shebang):

#!/usr/bin/env python2
import sys
import shutil

How can I use sed to get this?

#!/usr/bin/env python2
import os
import sys
import shutil

Solution

  • You want to prefix the line with the line you want to add.

    sed '1s|.*|#!/usr/bin/env python2\n&|'