Post by Greg WooledgePost by David ChristensenPerhaps Perl and the module String::ShellQuote ?
$ touch "name with spaces"
$ 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 ***@laalaa ~
$ touch "foo
2024-05-02 21:52:29 ***@laalaa ~
$ ls -l foo*
-rw-r--r-- 1 dpchrist dpchrist 0 May 2 21:52 'foo'$'\n''bar'
2024-05-02 21:52:36 ***@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 ***@laalaa ~
$ touch 'foo
2024-05-02 21:57:14 ***@laalaa ~
$ ls -l foo*
-rw-r--r-- 1 dpchrist dpchrist 0 May 2 21:57 'foo'$'\n''bar'
2024-05-02 21:57:18 ***@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.
Post by Greg WooledgeI 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