[Toybox] [PATCH] sh: pass "\" to the later app

Chet Ramey chet.ramey at case.edu
Thu Jun 1 08:20:33 PDT 2023


On 5/29/23 12:39 PM, Rob Landley wrote:

> But I'm still left with this divergence:
> 
>    $ ./sh -c 'echo abc\'
>    abc
>    $ bash -c 'echo abc\'
>    abc\

The backslash doesn't escape anything, EOF delimits the token and command,
and the backslash remains in place for echo to process (or not).

> Which is annoyingly magic because:
> 
>    $ bash << 'EOF'
>    > echo abc\
>    > EOF
>    abc

So think about this in two pieces: what the here-document does to generate
the input to the shell, and what the shell does with it.

Since the here-document delimiter is quoted, the `outer' shell doesn't do
anything special with the backslash-newline. If it were not quoted, the
backslash-newline would be removed, and the EOF would not delimit the
here-document.

So the shell is supplied input on file descriptor 0 that consists of a
single line (which ends with a newline):

echo abc\

which the shell reads. Since nothing is quoted, the backslash-newline gets
removed, the shell reads EOF and delimits the token and command, and echo
gets "abc" as its argument.

> 
> And also:
> 
>    $ echo 'echo abc\' > blah
>    $ cat blah
>    echo abc\
>    $ bash ./blah
>    abc

Same thing, the file ends with a backslash-newline that gets removed, EOF
delimits the token and command, echo gets "abc" and does the expected
thing.

> 
> So... do I special case -c here or what?

What's the special case? EOF (or EOS, really) always delimits tokens when
you're using -c command. Just the same as if you had a file that didn't
end with a newline.

> 
> Aha!
> 
>    $ bash -c $'echo abc\\'
>    abc\

There's no difference between this and 'echo abc\'.

>    $ bash -c $'echo abc\\\n'
>    abc

The backslash-newline gets removed. That always happens, regardless of
where the input is coming from.

> 
> So...
> 
>    $ echo -n 'echo abc\' | bash
>    abc
>    $ echo -n 'echo abc\' > blah
>    $ bash ./blah
>    abc

This looks inconsistent at first glance, I'll take a look.


> Nope, that's not it either, -c is still magic even when the file input hasn't
> got a newline.

What is `magic' about it?

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
		 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet at case.edu    http://tiswww.cwru.edu/~chet/



More information about the Toybox mailing list