Re: Including ' in a sed command
On Thu, Aug 7, 2025 at 7:18 PM Tim Woodall <debianuser@woodall.me.uk> wrote:
> This is part of a sed command in a bash script.
> sed 's/["'"'"']//'
> This matches " or '. It works.
> But it's 'unreadable' and almost impossible to find the typo if it's
> wrong.
> Is there a better way of writing it? the obvious '["\']' doesn't work.
More of a shell quoting question than sed, though sed also applies,
at least in part.
Well, you've got three relevant quoting mechanisms in POSIX(-like) shells,
including, e.g. bash, and dash.
' literally quotes, until next following ' character
" quotes, but is subject to variable interpolation, command substitution,
and depending upon shell, perhaps a bit more
\ quotes the immediately following character
So, there are various possibilities, e.g.:
If we limit ourselves to the above:
's/["'\'']//'
I typically use '\'' when I need a literal ' within a '' quoted string.
It's still not especially pretty, but it can scale well, including
programmatically adding/stripping - even multiple layers.
E.g. in ed(1)/vi(1)/ex(1):
s/'/'\\''/g
And use that for entire lines where applicable.
And likewise reversing it:
s/'\\''/'/g
Anyway, in addition to:
's/["'\'']//'
We have:
s/\[\"\'\]//
That might be among the most readable for that fairly simple case.
If there's more to your sed script/program,
can use ' to start/stop single quoting of it, as/where most appropriate.
Could also do:
"s/[\"']//"
That one's also quite readable.
Notably for:
s/["']//
All of the ["'] characters are special to the shell,
so, for them to be literal, need quote them, and they themselves
include two of our three applicable characters for relevant quoting.
So, using \ then makes it relatively readable, but if doing only
that, one puts in four of 'em to handle what needs quoting.
' characters around the whole thing works, except for ' itself,
so to handle that, have to break the single quoting with a
pair of '' characters, and then otherwise quote the '
where we want it literal, and that leaves " or \ as our options,
if we use \ that's a single character for each we need quoted,
or we use " and need a pair, plus have to worry about interpolation,
etc. and also deal with any " characters within.
Without going into what (GNU) sed itself offers,
other way we can approach it bit more with shell,
is have some shell variables that interpolate to what we want.
That might be more useful if there's a lot of such in the sed
script. So, e.g.:
sq=\'
dq=\"
sed "s/[$dq$sq]//"
Or for even possibly better readability:
sed "s/[${dq}${sq}]//"
And if we don't want outside of that scope, having additional shell
variables "pollute" our shell variable and possibly environment space,
and perhaps have conflicts,
could do such within a subshell, using ( and ), and set those variables
within, and have nothing else in the subshell other than that and
the sed command and its arguments.
And some alternative ways with sed and (more generally) Linux, while
still using sed:
set -f sedscript
And then your sedscript file contains:
s/["']//
If your program consists only of sed
(as, e.g. my
https://www.mpaoli.net/~michael/bin/ttt
which is a quite functional Tic-Tac-Toe program written in sed)
You could have an executable program which e.g. contains:
#!/usr/bin/sed -f
s/["']//
And then just use that, e.g.:
$ cat /tmp/tmp.RuKPIVgy29
#!/usr/bin/sed -f
s/["']//
$ printf '%s\n%s\n' 'ab'\''"cd' 'ab"'\''cd'
ab'"cd
ab"'cd
$ printf '%s\n%s\n' 'ab'\''"cd' 'ab"'\''cd' | /tmp/tmp.RuKPIVgy29
ab"cd
ab'cd
$
See also the start of my ttt program for some relevant notes about the -n
option in such case.
Reply to: