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

7 messages from 2011-06-20 to 2011-06-21. Participants: Michael J Gruber, Jonathan Nieder, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/27662

## Michael J Gruber, 2011-06-20 09:58

Subject: POSIX woes in t7810.87: dash bash or bash dash?
Message-ID: <4DFF19C4.5070003@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4DFF19C4.5070003%40drmicha.warpmail.net

```
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, 2011-06-20 11:04

Subject: Re: POSIX woes in t7810.87: dash bash or bash dash?
Message-ID: <20110620110440.GA29270@elie>
URL: https://gitlist.dev/e/20110620110440.GA29270%40elie
In-Reply-To: <4DFF19C4.5070003@drmicha.warpmail.net>

```
Hi Michael,

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.

> 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, 2011-06-20 17:13

Subject: Re: POSIX woes in t7810.87: dash bash or bash dash?
Message-ID: <20110620171307.GA5859@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20110620171307.GA5859%40sigill.intra.peff.net
In-Reply-To: <20110620110440.GA29270@elie>

```
On Mon, Jun 20, 2011 at 06:04:41AM -0500, Jonathan Nieder wrote:

> 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.

> > 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, 2011-06-20 21:46

Subject: Re: POSIX woes in t7810.87: dash bash or bash dash?
Message-ID: <7voc1sb2hr.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7voc1sb2hr.fsf%40alter.siamese.dyndns.org
In-Reply-To: <4DFF19C4.5070003@drmicha.warpmail.net>

```
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, 2011-06-20 23:24

Subject: Re: POSIX woes in t7810.87: dash bash or bash dash?
Message-ID: <20110620232425.GA617@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20110620232425.GA617%40sigill.intra.peff.net
In-Reply-To: <7voc1sb2hr.fsf@alter.siamese.dyndns.org>

```
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).

-Peff

```

## Junio C Hamano, 2011-06-20 23:31

Subject: Re: POSIX woes in t7810.87: dash bash or bash dash?
Message-ID: <7vwrgg9j1n.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vwrgg9j1n.fsf%40alter.siamese.dyndns.org
In-Reply-To: <20110620232425.GA617@sigill.intra.peff.net>

```
Jeff King <peff@peff.net> writes:

> 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, 2011-06-21 00:21

Subject: Re: POSIX woes in t7810.87: dash bash or bash dash?
Message-ID: <20110621002114.GA2050@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20110621002114.GA2050%40sigill.intra.peff.net
In-Reply-To: <7vwrgg9j1n.fsf@alter.siamese.dyndns.org>

```
On Mon, Jun 20, 2011 at 04:31:32PM -0700, Junio C Hamano wrote:

> >> +	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

```
