Search code examples
escapingfish

fish: escaping `^` in sed


In Fish shell, try to escape ^ in [^anything] in sed, however, it does not work.

I tried using single quotes ('s///'), double quotes ("s///", escapig via single or double slash (/), nothing works.

I’d like a solution for both single and double quotes in sed command.

$ echo 'cd $OLDPWD $argv' | sed $sed_options "s/cd \([\^ ]\) \$argv/cd \1/g" -

$ echo 'cd $OLDPWD $argv' | sed $sed_options 's/cd \([\^ ]\) \$argv/cd \1/g' -

Solution

  • In Fish shell, try to escape ^ in [^anything] in sed, however, it does not work.

    So, the thing is you don't need to escape the ^ for fish's benefit[0].

    If you want an inverted character set, you should just use it without a backslash.

    E.g.

    echo abc | sed -e 's/[^a]/+/g'
    

    will replace both the "b" and the "c" with a "+", because neither is an "a".

    I’d like a solution for both single and double quotes in sed command.

    This is the same in single- and double-quotes.

    echo 'cd $OLDPWD $argv' | sed $sed_options "s/cd \([^ ]\) \$argv/cd \1/g"
    

    [0] Technically versions before fish 3 used it to denote stderr redirections, but only at the start of a token, and only outside of quotes. None of which is applicable here.