Skip to contentSkip to conversation

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

DISCUSSION Michael J Gruber 4 participants 7 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

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

#1 lore raw

Hi Michael,

Quoted 6 lines · Michael J Gruber

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.

Quoted 2 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].

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

#2 lore raw
Quoted 11 lines · Jonathan Nieder
Quoted 6 lines · Michael J Gruber

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.

Quoted 8 lines
Quoted 2 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

#3 lore raw
In reply to Michael J Gruber

Thanks for noticing. Let's do this.

 t/t7810-grep.sh |    6 +++---
 1 files changed, 3 insertions(+), 3 deletions(-)
t/t7810-grep.sh +3 -3
index 69bd576..6379ad6 100755
@@ -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
 '
#4 lore raw
Quoted 5 lines · Junio C Hamano
t/t7810-grep.sh +0 -0
index 69bd576..6379ad6 100755

t/t7810-grep.sh test_expect_success LIBPCRE 'grep -P -v pattern' ' · line 663

Quoted 9 lines
t/t7810-grep.sh +3 -3
@@ -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

#5 lore raw
Quoted 9 lines · Jeff King
Quoted 7 lines · Junio C Hamano
t/t7810-grep.sh +0 -0
index 69bd576..6379ad6 100755
@@ -658,9 +658,9 @@ test_expect_success LIBPCRE 'grep -P -v pattern' '
 '
 
Quoted 12 lines

t/t7810-grep.sh test_expect_success LIBPCRE 'grep -P -v pattern' ' · line 663

Quoted 7 lines
t/t7810-grep.sh +3 -3
 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.

#6 lore raw
Quoted 11 lines · Junio C Hamano
Quoted 8 lines

t/t7810-grep.sh test_expect_success LIBPCRE 'grep -P -v pattern' ' · line 663

Quoted 3 lines
t/t7810-grep.sh +3 -0
+	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