• How do I escape a string that contains both single and double quotes?

    From Ottavio Caruso@21:1/5 to All on Thu Mar 17 10:16:21 2022
    Hi,


    I have a function that checks the occurrence of characters in a string:

    cw-sort ()
    {
    echo $1 | sed 's/./&\n/g' | sort | uniq -ic | sort -rn
    }


    This is all fine and dandy as long as there are no funny characters:

    $ cw-sort uyrwqoequwyroqiuwey8378429537uriewr
    4 w
    4 u
    4 r
    3 y
    3 q
    3 e
    2 o
    2 i
    2 8
    2 7
    2 3
    1 9
    1 5
    1 4
    1 2
    1

    When you throw single quotes in the mix, you have to escape them in
    double quotes:

    $ cw-sort "uyrwqoequwyroqiuwey83'''''78429537uriewr"
    5 '
    4 w
    4 u
    4 r
    3 y
    3 q
    3 e
    2 o
    2 i
    2 8
    2 7
    2 3
    1 9
    1 5
    1 4
    1 2
    1

    Likewise, if you throw double quotes in the mix, you have to escape them
    in single quotes:

    $ cw-sort 'uyrwqoequwyroqiuwey83"""""""""""78429537uriewr'
    11 "
    4 w
    4 u
    4 r
    3 y
    3 q
    3 e
    2 o
    2 i
    2 8
    2 7
    2 3
    1 9
    1 5
    1 4
    1 2
    1


    But what if you have both single and double quotes?

    $ cw-sort 'uyrwqoequwyroqiuwey83""""'''''78429537uriewr'

    $
    $ cw-sort "uyrwqoequwyroqiuwey83""""'''''78429537uriewr"
    5 '
    4 w
    4 u
    4 r
    3 y
    3 q
    3 e
    2 o
    2 i
    2 8
    2 7
    2 3
    1 9
    1 5
    1 4
    1 2
    1

    Wrapping the string in single quotes, makes the function choke. Wrapping
    the string in double quotes makes the double quotes disappear from the
    count.

    How do I get out of this?

    --
    Ottavio Caruso

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)