Re: realpath quoting
On 5/2/24 19:19, Greg Wooledge wrote:
On Thu, May 02, 2024 at 07:11:46PM -0700, David Christensen wrote:
Perhaps Perl and the module String::ShellQuote ?
2024-05-02 18:50:28 dpchrist@laalaa ~
$ touch "name with spaces"
2024-05-02 18:50:45 dpchrist@laalaa ~
$ touch "name with\nnewline"
You didn't create a name with a newline in it here. You created a name
with a backslash in it. If you wanted a newline, you would have to use
the $'...' quoting form (in bash).
touch $'name with\nnewline'
Thank you for the clarification.
RTFM bash(1):
QUOTING
...
Enclosing characters in double quotes preserves the literal
value of all characters within the quotes, with the exception of
$, `, \, and, when history expansion is enabled, !. ...
The backslash retains its special meaning only when followed
by one of the following characters: $, `, ", \, or <newline>.
...
Words of the form $'string' are treated specially. The word
expands to string, with backslash-escaped characters replaced
as specified by the ANSI C standard.
I found another way to obtain a file name containing a newline -- by
pressing <Enter> when typing a double-quoted string literal:
2024-05-02 21:52:23 dpchrist@laalaa ~
$ touch "foo
> bar"
2024-05-02 21:52:29 dpchrist@laalaa ~
$ ls -l foo*
-rw-r--r-- 1 dpchrist dpchrist 0 May 2 21:52 'foo'$'\n''bar'
2024-05-02 21:52:36 dpchrist@laalaa ~
$ perl -MString::ShellQuote -e 'print shell_quote(@ARGV), "\n"' foo*
'foo
bar'
It also seems to work for single-quoted string literals:
2024-05-02 21:57:08 dpchrist@laalaa ~
$ touch 'foo
> bar'
2024-05-02 21:57:14 dpchrist@laalaa ~
$ ls -l foo*
-rw-r--r-- 1 dpchrist dpchrist 0 May 2 21:57 'foo'$'\n''bar'
2024-05-02 21:57:18 dpchrist@laalaa ~
$ perl -MString::ShellQuote -e 'print shell_quote(@ARGV), "\n"' foo*
'foo
bar'
I am unable to find $'string' in the dash(1) man page (?). As I
typically write "#!/bin/sh" shell scripts, writing such to deal with
file names containing non-printing characters is going to baffle me.
I still insist that this is a workaround that should *not* be used
to try to cancel out quoting bugs in one's shell scripts. Just write
the shell scripts correctly in the first place.
I would if I could.
While I am also unable to write Perl scripts correctly in the first
place, the quoting rules are easier.
David
Reply to: