git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] test-lib.sh: use printf instead of echo

From
Jonathan Nieder <jrnieder@gmail.com>
Date
Mar 18, 2014, 22:18 UTC
Message-ID
<20140318221844.GA828@google.com>
In-Reply-To
<xmqq61nceidp.fsf@gitster.dls.corp.google.com>
Junio C Hamano wrote:
>> Uwe Storbeck wrote:
>>> +	printf '%s\n' "$@" | sed -e 's/^/#	/'
>
> This is wrong, isn't it?  Why do we want one line per item here?
Yes, Hannes caught the same, too.  Sorry for the sloppiness.

We currently use "echo" all over the place (e.g., 'echo "$path"' in git-sh-setup), and every time we fix it there is a chance of making mistakes. I wonder if it would make sense to add a helper to make the echo calls easier to replace:

-- >8 --
Subject: git-sh-setup: introduce sane_echo helper

Using 'echo' with arguments that might contain backslashes or "-e" or "-n" can produce confusing results that vary from platform to platform (e.g., dash always interprets \ escape sequences and echoes "-e" verbatim, whereas bash does not interpret \ escapes unless "-e" is passed as an argument to echo and suppresses the "-e" from its output).

Instead, we should use printf, which is more predictable:
	printf '%s\n' "$foo"; # Just prints $foo, on all platforms.

Blindly replacing echo with "printf '%s\n'" would not be good enough because that printf prints each argument on its own line. Provide a sane_echo helper that prints its arguments, space-delimited, on a single line, to make this easier to remember, and tweak 'say' and 'die_with_status' to illustrate how it is used.

No functional change intended.
Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>
---
 git-sh-setup.sh | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
diff --git i/git-sh-setup.sh w/git-sh-setup.sh
index 256c89a..f35b5b9 100644
--- i/git-sh-setup.sh
+++ w/git-sh-setup.sh
@@ -43,6 +43,10 @@ git_broken_path_fix () {
 
 # @@BROKEN_PATH_FIX@@
 
+sane_echo () {
+	printf '%s\n' "$*"
+}
+
 die () {
 	die_with_status 1 "$@"
 }
@@ -50,7 +54,7 @@ die () {
 die_with_status () {
 	status=$1
 	shift
-	printf >&2 '%s\n' "$*"
+	sane_echo >&2 "$*"
 	exit "$status"
 }
 
@@ -59,7 +63,7 @@ GIT_QUIET=
 say () {
 	if test -z "$GIT_QUIET"
 	then
-		printf '%s\n' "$*"
+		sane_echo "$*"
 	fi
 }
 
Previous: Junio C HamanoNext: Junio C Hamano
Message 4 of 14 in “test-lib.sh: use printf instead of echo”
  1. test-lib.sh: use printf instead of echoUwe Storbeck, Mar 14, 2014
  2. Jonathan NiederMar 15, 2014
  3. Junio C HamanoMar 17, 2014
  4. Jonathan NiederMar 18, 2014
  5. Junio C HamanoMar 19, 2014
  6. David KastrupMar 19, 2014
  7. Junio C HamanoMar 19, 2014
  8. Jonathan NiederMar 20, 2014
  9. Junio C HamanoMar 20, 2014
  10. Johannes SixtMar 15, 2014
  11. Uwe StorbeckMar 17, 2014
  12. test-lib.sh: use printf instead of echoUwe Storbeck, Mar 17, 2014
  13. test-lib.sh: do not "echo" externally supplied stringsUwe Storbeck, Mar 18, 2014
  14. Junio C HamanoMar 18, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.