This will work:
sed -e '\|somevar|s|foo|bar|'
The man
page of GNU sed is pretty clear about this:
/regexp/
Match lines matching the regular expression regexp.
\cregexpc
Match lines matching the regular expression regexp. The c may
be any character.
That is, the c
may be any character, but the starting \
is mandatory.
I don't have a FreeBSD around, but according to @bonsaiviking the man
page there is also very clear:
The opening delimiter needs to be preceded by a backslash unless it is a slash.
On the other hand in OSX this is not clear at all:
In a context address, any character other than a backslash (``\'')
or newline character may be used to delimit the regular expression.
Also, putting a backslash character before the delimiting character
causes the character to be treated literally. For example, in the
context address \xabc\xdefx, the RE delimiter is an ``x'' and the
second ``x'' stands for itself, so that the regular expression is
``abcxdef''.
Notice that the example there uses \xpatternx
instead of just xpatternx
. That's all the clue it gives, it doesn't make it clear that xpatternx
won't work.
Based on the argument of @that-other-guy, it makes sense that sed
(and other languages like perl
as @Birei pointed out) need this extra clue to work correctly.