We normally see people complaining about the unknown option to s'
error in sed
when they want to use a pattern that contains the sed
delimiter.
For example, if we are using /
:
$ var="hel/lo"
$ sed "s/a/$var/g" <<< "haha"
sed: -e expression #1, char 9: unknown option to `s'
So we advise to use another delimiter, for example |
:
$ sed "s|a|$var|g" <<< "haha"
hhel/lohhel/lo
However, I want to know what are the possible delimiters sed
can accept... since it seems to be almost any character including regex-like ones (*
, ?
, .
, ...)!
In my sed (GNU sed) 4.2.2
:
$ sed 's/a/b/g' <<< "haha"
hbhb
$ sed 's_a_b_g' <<< "haha"
hbhb
$ sed 's#a#b#g' <<< "haha"
hbhb
$ sed 's$a$b$g' <<< "haha"
hbhb
$ sed 's?a?b?g' <<< "haha"
hbhb
$ sed 's*a*b*g' <<< "haha"
hbhb
$ sed 's-a-b-g' <<< "haha"
hbhb
$ sed 's.a.b.g' <<< "haha"
hbhb
$ sed 'sXaXbXg' <<< "haha"
hbhb
$ sed 'sxaxbxg' <<< "haha"
hbhb
$ sed 's1a1b1g' <<< "haha"
hbhb
Even a
works here if it is escaped:
$ sed 'saaabag' <<< "haha"
sed: -e expression #1, char 5: unknown option to `s'
$ sed 'sa\aabag' <<< "haha"
hbhb
Is there any specification for this?
Perhaps the closest to a standard, the POSIX/IEEE Open Group Base Specification says:
[2addr] s/BRE/replacement/flags
Substitute the replacement string for instances of the BRE in the pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the BRE and the replacement. Within the BRE and the replacement, the BRE delimiter itself can be used as a literal character if it is preceded by a backslash."