I stumbled upon a little problem with the following sed command that strips the leading and trailing space characters of a line and encapsulates it in double-quotes:
printf '%s\n' ' hello ' ' hello' 'hello ' hello | sed -E 's/^ *| *$/"/g'
The results are:
- on Linux:
"hello"
"hello"
"hello"
"hello"
- on macOS:
"hello"
"hello
"hello"
"hello
- on FreeBSD
"hello"
"
"hello"
"hello"
I'm not really looking for a workaround because I got these ones that work in all platforms (I'm open to other proposals though):
sed 's/^ */"/;s/ *$/"/'
awk '{gsub(/^ *| *$/,"\"")}1' # the culprit works fine with awk
My question is: Is my understanding of the sed command wrong or can this be considered a bug in macOS and FreeBSD sed implementations?