Re: [List] Re: Nice easy sed question

From: Bob Proulx <bob_at_proulx.com>
Date: Fri, 12 Sep 2025 22:46:12 UTC
Arthur Chance wrote:
> Presuming you're working with sh or bash (as opposed to {t,}csh) and
> it's having an actual newline that's annoying, you can always use sh's
> dollar single quote strings. Then you get the alternate annoyance of
> backslash doubling instead.
>
> sed  -i.bak  -e $'/Line 2/a\\\nNew Line' example.txt

This is a good suggestion.  However it does turn on escape sequence
interpretation.

Using $'...' to enable escape sequences does work in both BSD and GNU
shells but then you must also be aware that escape sequences are
enabled and deal with escaping the escapes when using them with sed
commands.

     Dollar-Single Quotes
             Enclosing characters between $' and ' preserves the literal
             meaning of all characters except backslashes and single quotes.
             A backslash introduces a C-style escape sequence:
             \a          Alert (ring the terminal bell)
             \b          Backspace
             \cc         The control character denoted by ^c in stty(1).  If c
                         is a backslash, it must be doubled.
             \e          The ESC character (ASCII 0x1b)
             \f          Formfeed
             \n          Newline
             \r          Carriage return
             \t          Horizontal tab
             \v          Vertical tab
             \\          Literal backslash
             \'          Literal single-quote
             \"          Literal double-quote
             \nnn        The byte whose octal value is nnn (one to three
                         digits)
             \xnn        The byte whose hexadecimal value is nn (one or more
                         digits only the last two of which are used)
             \unnnn      The Unicode code point nnnn (four hexadecimal digits)
             \Unnnnnnnn  The Unicode code point nnnnnnnn (eight hexadecimal
                         digits)

Dealing with the escape sequences above when I am also using
backslashes in sed is what keeps me from doing it this way.  But it is
a good suggestion and does work.

The FreeBSD sh shell is not a strict POSIX shell and implements this
ksh-ism so strictly speaking that is not portable but it does work.
It will work on both BSD and GNU systems though so maybe least
objectionable to you.

This is what POSIX has to say about it.

    The '$' character is used to introduce parameter expansion, command
    substitution, or arithmetic evaluation. If an unquoted '$' is followed
    by a character that is not one of the following:

        A numeric character

        The name of one of the special parameters (see Special Parameters)

        A valid first character of a variable name

        A <left-curly-bracket> ( '{' )

        A <left-parenthesis>

    the result is unspecified.

The result of $'...' such as $'\n' is strictly speaking "unspecified"
behavior by the standard.

Bob