Search code examples
sedgnushbsd

What is the purpose of the "-" in sh script line: ext="$(echo $ext | sed 's/\./\\./' -)"


I am porting a sh script that was apparently written using GNU implementation of sed to BSD implementation of sed. The exact line in the script with the original comment are:

# escape dot in file extension to grep it
ext="$(echo $ext | sed 's/\./\\./' -)"

I am able to reproduce a results with the following (obviously I am not exhausting all possibilities values for ext) :

ext=.h; ext="$(echo $ext | sed 's/\./\\./' -)"; echo [$ext]

Using GNU's implementation of sed the following is returned:

[\.h]

Using BSD's implementation of sed the following is returned:

sed: -: No such file or directory
[]

Executing ext=.h; ext="$(echo $ext | sed 's/\./\\./')"; echo [$ext] returns [\.h] for both implementation of sed.

I have looked at both GNU and BSD's sed's man page have not found anything about the trailing "-". Googling for sed with a "-" is not very fruitful either.

Is the "-" a typo? Is the "-" needed for some an unexpected value of $ext? Is the issue not with sed, but rather with sh?

Can someone direct me to what I should be looking at, or even better, explain what the purpose of the "-" is?


Solution

  • On my system, that syntax isn't documented in the man page, but it is in the 'info' page:

    sed OPTIONS... [SCRIPT] [INPUTFILE...]

    If you do not specify INPUTFILE, or if INPUTFILE is -',sed' filters the contents of the standard input.

    Given that particular usage, I think you could leave off the '-' and it should still work.