Skip to contentSkip to conversation

t/t9902-completion.sh: backslashes in echo

PATCH In review Marcel Telka 3 participants 10 messages May 17, 2024 – May 23, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 t/t9902-completion.sh: backslashes in echo

The usage of backslashes in echo is not portable. Since some tests tries to output strings containing '\b' it is safer to use printf here. The usage of printf instead of echo is also preferred by POSIX.

---
 t/t9902-completion.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t9902-completion.sh +1 -1
index 963f865f27..ed3d03367e 100755
@@ -73,7 +73,7 @@ _get_comp_words_by_ref ()
 print_comp ()
 {
 	local IFS=$'\n'
-	echo "${COMPREPLY[*]}" > out
+	printf '%s\n' "${COMPREPLY[*]}" > out
 }
 
 run_completion ()
#1 lore raw
Quoted 14 lines · Marcel Telka

The usage of backslashes in echo is not portable. Since some tests tries to output strings containing '\b' it is safer to use printf here. The usage of printf instead of echo is also preferred by POSIX.

---
 t/t9902-completion.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t9902-completion.sh +0 -0
index 963f865f27..ed3d03367e 100755

t/t9902-completion.sh _get_comp_words_by_ref () · line 77

Quoted 6 lines
t/t9902-completion.sh +1 -1
@@ -73,7 +73,7 @@ _get_comp_words_by_ref ()
 print_comp ()
 {
 	local IFS=$'\n'
-	echo "${COMPREPLY[*]}" > out
+	printf '%s\n' "${COMPREPLY[*]}" > out
 }

This has cooked in 'next' for some time already, and I'll merge this down to 'master' anyway, but this being a script very much speicific to bash whose built-in echo we are using, the portability argument of "echo" made in the proposed log message does not quite apply to this patch.

Thanks.

#2 lore raw
Quoted 20 lines · Junio C Hamano
Quoted 18 lines · Marcel Telka

The usage of backslashes in echo is not portable. Since some tests tries to output strings containing '\b' it is safer to use printf here. The usage of printf instead of echo is also preferred by POSIX.

---
 t/t9902-completion.sh | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
t/t9902-completion.sh +0 -1
index 963f865f27..ed3d03367e 100755
@@ -73,7 +73,7 @@ _get_comp_words_by_ref ()
 print_comp ()
 {
 	local IFS=$'\n'
-	echo "${COMPREPLY[*]}" > out
Quoted 8 lines

t/t9902-completion.sh _get_comp_words_by_ref () · line 77

Quoted 2 lines
t/t9902-completion.sh +1 -0
+	printf '%s\n' "${COMPREPLY[*]}" > out
 }

This has cooked in 'next' for some time already, and I'll merge this down to 'master' anyway, but this being a script very much speicific to bash whose built-in echo we are using, the portability argument of "echo" made in the proposed log message does not quite apply to this patch.

Could you please help me to understand how the bash is used to run the script?

$ head -n 1 t/t9902-completion.sh
#!/bin/sh
$

Thank you.

Signature · 4 lines
+-------------------------------------------+
| Marcel Telka   e-mail:   [email protected]  |
|                homepage: http://telka.sk/ |
+-------------------------------------------+
#3 lore raw
Quoted 12 lines · Marcel Telka
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Marcel Telka

The usage of backslashes in echo is not portable. Since some tests tries to output strings containing '\b' it is safer to use printf here. The usage of printf instead of echo is also preferred by POSIX.

This has cooked in 'next' for some time already, and I'll merge this down to 'master' anyway, but this being a script very much speicific to bash whose built-in echo we are using, the portability argument of "echo" made in the proposed log message does not quite apply to this patch.

Quoted 5 lines

Could you please help me to understand how the bash is used to run the script?

$ head -n 1 t/t9902-completion.sh
#!/bin/sh

Looking a bit farther in t9902, you'll find:

    . ./lib-bash.sh

And if you look inside lib-bash.sh, you'll see that, if the currently-running shell is not Bash, it aborts the current script and re-runs it under Bash.

But Junio's point was that t9902 is dedicated to testing Bash-specific functionality, so the commit message's justification to avoid this non-POSIX behavior isn't necessarily a good justification for the change. (He wasn't saying that the change itself was unwelcome, just that the commit message wasn't convincing.)

#4 lore raw
Quoted 19 lines · Eric Sunshine
Quoted 12 lines · Marcel Telka
Quoted 10 lines · Junio C Hamano
Quoted 3 lines · Marcel Telka

The usage of backslashes in echo is not portable. Since some tests tries to output strings containing '\b' it is safer to use printf here. The usage of printf instead of echo is also preferred by POSIX.

This has cooked in 'next' for some time already, and I'll merge this down to 'master' anyway, but this being a script very much speicific to bash whose built-in echo we are using, the portability argument of "echo" made in the proposed log message does not quite apply to this patch.

Quoted 5 lines

Could you please help me to understand how the bash is used to run the script?

$ head -n 1 t/t9902-completion.sh
#!/bin/sh
Quoted 7 lines

Looking a bit farther in t9902, you'll find:

    . ./lib-bash.sh

And if you look inside lib-bash.sh, you'll see that, if the currently-running shell is not Bash, it aborts the current script and re-runs it under Bash.

Thanks for the pointer!

Interesting. I came to this issue because the test failed here due to the echo. So apparently the re-run under bash didn't happen here for some reason or bash's echo behaved differently? I need to dig into it again to find what's going on.

Quoted 5 lines

But Junio's point was that t9902 is dedicated to testing Bash-specific functionality, so the commit message's justification to avoid this non-POSIX behavior isn't necessarily a good justification for the change. (He wasn't saying that the change itself was unwelcome, just that the commit message wasn't convincing.)

Sure. I understand the point. I just had an impression that the test is running under POSIX sh (/bin/sh), not bash, because it failed here (see above).

OTOH, it seems that the fix is maybe not needed because it is solving a non-problem.

Thank you.

Signature · 4 lines
+-------------------------------------------+
| Marcel Telka   e-mail:   [email protected]  |
|                homepage: http://telka.sk/ |
+-------------------------------------------+
#5 lore raw
Quoted 4 lines · Marcel Telka

Interesting. I came to this issue because the test failed here due to the echo. So apparently the re-run under bash didn't happen here for some reason or bash's echo behaved differently? I need to dig into it again to find what's going on.

Okay, I found the cause. Bash on OpenIndiana is compiled with --enable-xpg-echo-default so escape sequencies are expanded by default.

Quoted 2 lines

OTOH, it seems that the fix is maybe not needed because it is solving a non-problem.

There is a problem, but definitely the justification in the commit message is not accurate because we do not care about POSIX here at all. Also maybe it would be better/simpler to use echo -E instead of printf, but I'm not sure here.

Thanks.

Signature · 4 lines
+-------------------------------------------+
| Marcel Telka   e-mail:   [email protected]  |
|                homepage: http://telka.sk/ |
+-------------------------------------------+
#6 lore raw
Quoted 9 lines · Marcel Telka
Quoted 4 lines · Marcel Telka

Interesting. I came to this issue because the test failed here due to the echo. So apparently the re-run under bash didn't happen here for some reason or bash's echo behaved differently? I need to dig into it again to find what's going on.

Okay, I found the cause. Bash on OpenIndiana is compiled with --enable-xpg-echo-default so escape sequencies are expanded by default.

Quoted 7 lines
Quoted 2 lines

OTOH, it seems that the fix is maybe not needed because it is solving a non-problem.

There is a problem, but definitely the justification in the commit message is not accurate because we do not care about POSIX here at all. Also maybe it would be better/simpler to use echo -E instead of printf, but I'm not sure here.

Reading again the commit message I proposed[1] I think it is okay.

[1] https://lore.kernel.org/git/Zkdk7R9GIfsyQjkc@telcontar/

Signature · 4 lines
+-------------------------------------------+
| Marcel Telka   e-mail:   [email protected]  |
|                homepage: http://telka.sk/ |
+-------------------------------------------+
#7 lore raw
In reply to Marcel Telka
Quoted 10 lines · Marcel Telka

Okay, I found the cause. Bash on OpenIndiana is compiled with --enable-xpg-echo-default so escape sequencies are expanded by default.

Quoted 2 lines

OTOH, it seems that the fix is maybe not needed because it is solving a non-problem.

There is a problem, but definitely the justification in the commit message is not accurate because we do not care about POSIX here at all. Also maybe it would be better/simpler to use echo -E instead of printf, but I'm not sure here.

How "portable" is "echo -E"? It apparently is not listd in [*], but it should probably not matter as we are doing this in bash.

printf is a kosher way whose behaviour is pretty well standardized especially with respect to "%s". As I said that I was going to merge it down to 'master' already, it is now part of 'master'. Use of printf there may hopefully educate folks to think twice before using 'echo' on unknown data.

By the way, it makes me feel funny that we are talking about "POSIX portability" when reviewing a change like this:

         print_comp ()
         {
                local IFS=$'\n'
        -	echo "${COMPREPLY[*]}" > out
        +	printf '%s\n' "${COMPREPLY[*]}" > out
         }

With "${ARRAY[*]}", POSIX portability is totally out of the water anyway, regardless of the echo/printf issue ;-).

[Reference]

* https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html

#8 lore raw
Quoted 6 lines · Junio C Hamano
Quoted 4 lines · Marcel Telka

There is a problem, but definitely the justification in the commit message is not accurate because we do not care about POSIX here at all. Also maybe it would be better/simpler to use echo -E instead of printf, but I'm not sure here.

Quoted 8 lines

How "portable" is "echo -E"? It apparently is not listd in [*], but it should probably not matter as we are doing this in bash.

printf is a kosher way whose behaviour is pretty well standardized especially with respect to "%s". As I said that I was going to merge it down to 'master' already, it is now part of 'master'. Use of printf there may hopefully educate folks to think twice before using 'echo' on unknown data.

Indeed. Seeingecho $VAR used always makes my reading hiccup since it's never immediately clear (without consulting additional context) whether the value of VAR starts with a hyphen or has embedded escapes. On the other hand, printf "%s" $VAR doesn't suffer from this problem, so this change is welcome regardless.

#9 lore raw
In reply to Junio C Hamano
Quoted 8 lines · Junio C Hamano
Quoted 6 lines · Marcel Telka

Okay, I found the cause. Bash on OpenIndiana is compiled with --enable-xpg-echo-default so escape sequencies are expanded by default.

Quoted 2 lines

OTOH, it seems that the fix is maybe not needed because it is solving a non-problem.

Quoted 7 lines
Quoted 4 lines

There is a problem, but definitely the justification in the commit message is not accurate because we do not care about POSIX here at all. Also maybe it would be better/simpler to use echo -E instead of printf, but I'm not sure here.

How "portable" is "echo -E"? It apparently is not listd in [*], but it should probably not matter as we are doing this in bash.

Agreed.

Quoted 16 lines

printf is a kosher way whose behaviour is pretty well standardized especially with respect to "%s". As I said that I was going to merge it down to 'master' already, it is now part of 'master'. Use of printf there may hopefully educate folks to think twice before using 'echo' on unknown data.

By the way, it makes me feel funny that we are talking about "POSIX portability" when reviewing a change like this:

         print_comp ()
         {
                local IFS=$'\n'
        -	echo "${COMPREPLY[*]}" > out
        +	printf '%s\n' "${COMPREPLY[*]}" > out
         }
Quoted 2 lines

With "${ARRAY[*]}", POSIX portability is totally out of the water anyway, regardless of the echo/printf issue ;-).

The commit message does not say that this change is to make the code POSIX portable ;-). It silently assumes the portability is related to bash only.

Signature · 4 lines
+-------------------------------------------+
| Marcel Telka   e-mail:   [email protected]  |
|                homepage: http://telka.sk/ |
+-------------------------------------------+