threads / discuss / 27662

POSIX woes in t7810.87: dash bash or bash dash?

Subject: POSIX woes in t7810.87: dash bash or bash dash?

## tl;dr

7 messages between Jun 20, 2011 and Jun 21, 2011.

replies: 6people: 4as markdown or json

Michael J Gruber· Jun 20, 2011, 09:58 UTC · lore
GNU bash, Version 4.2.10(1)-release (x86_64-redhat-linux-gnu):

echo "a\"b\n\"c" a"b\n"c

dash-0.5.6:

echo "a\"b\n\"c" a"b "c

t7810.87 breaks with dash because of this. Escaping the \n does not make the test work with both either. So I'd like to know which one's right and (independently) how to make it work for both...

Cheers, Michael

Jonathan Nieder· Jun 20, 2011, 11:04 UTC · re: Michael J Gruber · lore

Re: POSIX woes in t7810.87: dash bash or bash dash?

Hi Michael,
Michael J Gruber wrote:
Show 6 quoted lines
> echo "a\"b\n\"c"
> a"b
> "c
>
> t7810.87 breaks with dash because of this. Escaping the \n does not make
> the test work with both either.

This seems to have slipped by because testing it requires USE_LIBPCRE to be set. Thanks for catching it.

> So I'd like to know which one's right
> and (independently) how to make it work for both...
Both are right.  The simplest fix is to use printf, as in
	printf "%s\n" "a\"b\n\"c"
See [1] and [2].

Regards, Jonathan

[1] http://www.gnu.org/s/hello/manual/autoconf/Limitations-of-Builtins.html#echo [2] http://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

Jeff King· Jun 20, 2011, 17:13 UTC · re: Jonathan Nieder · lore

Re: POSIX woes in t7810.87: dash bash or bash dash?

On Mon, Jun 20, 2011 at 06:04:41AM -0500, Jonathan Nieder wrote:
Show 11 quoted lines
> Michael J Gruber wrote:
> 
> > echo "a\"b\n\"c"
> > a"b
> > "c
> >
> > t7810.87 breaks with dash because of this. Escaping the \n does not make
> > the test work with both either.
> 
> This seems to have slipped by because testing it requires USE_LIBPCRE
> to be set.  Thanks for catching it.

Yeah, I test with dash, too, and missed it because I hadn't enabled pcre.

Show 8 quoted lines
> > So I'd like to know which one's right
> > and (independently) how to make it work for both...
> 
> Both are right.  The simplest fix is to use printf, as in
> 
> 	printf "%s\n" "a\"b\n\"c"
> 
> See [1] and [2].
Yep. We've dealt with this before and used printf as the solution. E.g.:
  938791c (git-rebase--interactive.sh: use printf instead of echo to print commit message, 2010-07-22)
  4d2e283 (git-am: re-fix the diag message printing, 2009-01-18)
  a23bfae (More echo "$user_message" fixes., 2007-05-26)
  4b7cc26 (git-am: use printf instead of echo on user-supplied strings, 2007-05-25)
-Peff
Junio C Hamano· Jun 20, 2011, 21:46 UTC · re: Michael J Gruber · lore

Re: POSIX woes in t7810.87: dash bash or bash dash?

Thanks for noticing. Let's do this.
 t/t7810-grep.sh |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
index 69bd576..6379ad6 100755
--- a/t/t7810-grep.sh
+++ b/t/t7810-grep.sh
@@ -658,9 +658,9 @@ test_expect_success LIBPCRE 'grep -P -v pattern' '
 '
 
 test_expect_success LIBPCRE 'grep -P -i pattern' '
-	{
-		echo "hello.c:	printf(\"Hello world.\n\");"
-	} >expected &&
+	cat >expected <<-EOF &&
+	hello.c:	printf("Hello world.\n");
+	EOF
 	git grep -P -i "PRINTF\([^\d]+\)" hello.c >actual &&
 	test_cmp expected actual
 '
Jeff King· Jun 20, 2011, 23:24 UTC · re: Junio C Hamano · lore

Re: POSIX woes in t7810.87: dash bash or bash dash?

On Mon, Jun 20, 2011 at 02:46:08PM -0700, Junio C Hamano wrote:
Show 14 quoted lines
> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
> index 69bd576..6379ad6 100755
> --- a/t/t7810-grep.sh
> +++ b/t/t7810-grep.sh
> @@ -658,9 +658,9 @@ test_expect_success LIBPCRE 'grep -P -v pattern' '
>  '
>  
>  test_expect_success LIBPCRE 'grep -P -i pattern' '
> -	{
> -		echo "hello.c:	printf(\"Hello world.\n\");"
> -	} >expected &&
> +	cat >expected <<-EOF &&
> +	hello.c:	printf("Hello world.\n");
> +	EOF

Do you need to quote EOF to suppress expansion in the here document? Both bash and dash seem to pass unknown backslash-escapes like "\n" through unharmed, but I don't know if that is portable (they do both munge known escapes like "\\", of course).

-Peff
Junio C Hamano· Jun 20, 2011, 23:31 UTC · re: Jeff King · lore

Re: POSIX woes in t7810.87: dash bash or bash dash?

Jeff King <peff@peff.net> writes:
Show 21 quoted lines
> On Mon, Jun 20, 2011 at 02:46:08PM -0700, Junio C Hamano wrote:
>
>> diff --git a/t/t7810-grep.sh b/t/t7810-grep.sh
>> index 69bd576..6379ad6 100755
>> --- a/t/t7810-grep.sh
>> +++ b/t/t7810-grep.sh
>> @@ -658,9 +658,9 @@ test_expect_success LIBPCRE 'grep -P -v pattern' '
>>  '
>>  
>>  test_expect_success LIBPCRE 'grep -P -i pattern' '
>> -	{
>> -		echo "hello.c:	printf(\"Hello world.\n\");"
>> -	} >expected &&
>> +	cat >expected <<-EOF &&
>> +	hello.c:	printf("Hello world.\n");
>> +	EOF
>
> Do you need to quote EOF to suppress expansion in the here document?
> Both bash and dash seem to pass unknown backslash-escapes like "\n"
> through unharmed, but I don't know if that is portable (they do both
> munge known escapes like "\\", of course).

I do not think that is strictly necessary, as we are not in the corner of non-portable echo behaviour anymore, but I guess it wouldn't hurt.

Jeff King· Jun 21, 2011, 00:21 UTC · re: Junio C Hamano · lore

Re: POSIX woes in t7810.87: dash bash or bash dash?

On Mon, Jun 20, 2011 at 04:31:32PM -0700, Junio C Hamano wrote:
Show 11 quoted lines
> >> +	cat >expected <<-EOF &&
> >> +	hello.c:	printf("Hello world.\n");
> >> +	EOF
> >
> > Do you need to quote EOF to suppress expansion in the here document?
> > Both bash and dash seem to pass unknown backslash-escapes like "\n"
> > through unharmed, but I don't know if that is portable (they do both
> > munge known escapes like "\\", of course).
> 
> I do not think that is strictly necessary, as we are not in the corner of
> non-portable echo behaviour anymore, but I guess it wouldn't hurt.

I think my brain is fried from using too many almost-shell-compatible quoting languages. For example, unknown escape sequences in C get their backslash removed and the sequence used literally (at least by gcc; I couldn't find anything definite in C99 on this).

But actually, POSIX is quite clear that a backslash before anything besides:

  $   `   "   \   <newline>
is just a backslash, and gets included literally.
-Peff

← back to recent threads