allenap / shell-quote

A Rust library for shell quoting strings, e.g. for interpolating into a Bash script. This is not as simple as most people think!
Apache License 2.0
5 stars 2 forks source link

ANSI-C quotes applied to strings without ANSI-C escape sequences #8

Open GeorgeHahn opened 6 months ago

GeorgeHahn commented 6 months ago

Hi, thanks for the great library!

I've noticed that some strings are unnecessarily quoted for ANSI-C escape sequences. This seems to happen on strings that contain an equal sign. For example, the string --arg=var will be quoted as $'--arg=var'. This is valid and doesn't cause any issues, but it does appear to be unnecessary.

I wasn't familiar with this style of quoting. It is covered in the bash docs here:

3.1.2.4 ANSI-C Quoting

Character sequences of the form $’string’ are treated as a special kind of single quotes. The sequence expands to string, with backslash-escaped characters in string replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

\a alert (bell)

\b backspace ... The expanded result is single-quoted, as if the dollar sign had not been present.

(source)

allenap commented 5 months ago

Yeah, it is a bit weird/confusing. I think I chose this style because of Unicode support:

\uHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHH (one to four hex digits)

\UHHHHHHHH the Unicode (ISO/IEC 10646) character whose value is the hexadecimal value HHHHHHHH (one to eight hex digits)

Perhaps the library could optimise so that it only uses $'...' style when it needs this support 🤔