sed in-place flag that works both on Mac (BSD) and Linux

后端 未结 13 2354
心在旅途
心在旅途 2020-11-22 08:18

Is there an invocation of sed todo in-place editing without backups that works both on Linux and Mac? While the BSD sed shipped with OS X seems to

13条回答
  •  猫巷女王i
    2020-11-22 08:49

    Steve Powell's answer is quite correct, consulting the MAN page for sed on OSX and Linux (Ubuntu 12.04) highlights the in-compatibility within 'in-place' sed usage across the two operating systems.

    JFYI, there should be no space between the -i and any quotes (which denote an empty file extension) using the Linux version of sed, thus

    sed Linux Man Page

    #Linux
    sed -i"" 
    

    and

    sed OSX Man page

    #OSX (notice the space after the '-i' argument)
    sed -i "" 
    

    I got round this in a script by using an alias'd command and the OS-name output of 'uname' within a bash 'if'. Trying to store OS-dependant command strings in variables was hit and miss when interpreting the quotes. The use of 'shopt -s expand_aliases' is necessary in order to expand/use the aliases defined within your script. shopt's usage is dealt with here.

提交回复
热议问题