Pop-Up Thingie
Sidebar
>>> Magnum BBS <<<
Home
Forum
Files
Dark
Log in
Username
Password
Sidebar
Forum
Usenet
COMP.UNIX.SHELL
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)
Who's Online
Recent Visitors
Furryboy
Sat Jun 6 10:56:29 2026
from
Romania, Galati
via
SSH
Centurion
Fri Jun 5 22:28:01 2026
from
Berea, Ohio
via
Telnet
Ab Cadd
Fri Jun 5 17:52:51 2026
from
Sheboygan, Wi
via
Telnet
Gwylbert
Fri Jun 5 06:28:52 2026
from
Sydney, Nsw
via
Telnet
Centurion
Thu Jun 4 23:42:23 2026
from
Berea, Ohio
via
Telnet
Michal Wronka
Thu Jun 4 23:19:58 2026
from
Wroclaw, Poland
via
Telnet
Michal Wronka
Thu Jun 4 23:17:20 2026
from
Wroclaw, Poland
via
SSH
Michal Wronka
Thu Jun 4 23:13:51 2026
from
Wroclaw, Poland
via
SSH
System Info
Sysop:
Keyop
Location:
Huddersfield, West Yorkshire, UK
Users:
714
Nodes:
16 (
2
/
14
)
Uptime:
141:49:56
Calls:
12,088
Calls today:
1
Files:
14,998
Messages:
6,517,448