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

Rob Landley rob at landley.net
Mon May 29 09:39:16 PDT 2023


On 5/29/23 02:19, Mingliang HU 胡明亮 wrote:
> Here is failed case for “echo -e "a\n\b\nc\td".
> 
> The expected result is:
> 
> a
> 
> c       d

You're missing a "b" there, but I get the general idea.

> The current result is:
> 
> anbnctd

Yeah, that's wrong.

> The patch let sh pass “\” to “echo”. Then it works fine.

I did a slightly more intrusive version than your patch (changing some of the
code around it, commit 23fc1ecab1b4), and now your test works:

  $ ./sh -c 'echo -e "a\nb\nc\td"'
  a
  b
  c	d

And also:

  $ echo ./sh -c "echo 'abc\'"
  ./sh -c echo 'abc\'
  $ ./sh -c "echo 'abc\'"
  abc\

But I'm still left with this divergence:

  $ ./sh -c 'echo abc\'
  abc
  $ bash -c 'echo abc\'
  abc\

Which is annoyingly magic because:

  $ bash << 'EOF'
  > echo abc\
  > EOF
  abc

And also:

  $ echo 'echo abc\' > blah
  $ cat blah
  echo abc\
  $ bash ./blah
  abc

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

Aha!

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

So...

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

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

Chet! What's up with this? I do not understand...

> Mingliang

Thanks,

Rob

P.S. yes there's a trailing space on the "abc " in toybox's last output there,
which is because my "append a line, oops there was no line" logic in the call to
parse_line() in do_source() (currently line 4084) delivers a " " instead of ""
to flush stuff, because completely blank lines get discarded too early in
parse_line(). I need to go through and fix that, here's a test for what else
THAT breaks:

  $ echo $'echo abc\\\n\necho def\n' | bash
  abc
  def
  $ echo $'echo abc\\\n\necho def\n' | ./sh
  abcecho def

It's on the todo list...


More information about the Toybox mailing list