threads / patch / 6394

patchReplace "echo -n" with printf in shell scripts.

Subject: [PATCH] Replace "echo -n" with printf in shell scripts.

## tl;dr

11 messages between Jan 16, 2007 and Jan 18, 2007. Diffs are folded; open one to read it.

replies: 10people: 7as markdown or json

Jason Riedy· Jan 16, 2007, 01:31 UTC · lore

Not all echos know -n. This was causing a test failure in t5401-update-hooks.sh, but not t3800-mktag.sh for some reason.

Signed-off-by: Jason Riedy <ejr@cs.berkeley.edu>
---
 contrib/remotes2config.sh |    2 +-
 git-quiltimport.sh        |    2 +-
 git-reset.sh              |    2 +-
 t/t3800-mktag.sh          |    2 +-
 t/t5401-update-hooks.sh   |    4 ++--
 t/t6023-merge-file.sh     |    2 +-
 6 files changed, 7 insertions(+), 7 deletions(-)
Show changes to 6 files +7 −7

contrib/remotes2config.sh, git-quiltimport.sh, git-reset.sh, t/t3800-mktag.sh, t/t5401-update-hooks.sh, t/t6023-merge-file.sh

diff --git a/contrib/remotes2config.sh b/contrib/remotes2config.sh
index 25901e2..b996996 100644
--- a/contrib/remotes2config.sh
+++ b/contrib/remotes2config.sh
@@ -11,7 +11,7 @@ if [ -d "$GIT_DIR"/remotes ]; then
 	{
 		cd "$GIT_DIR"/remotes
 		ls | while read f; do
-			name=$(echo -n "$f" | tr -c "A-Za-z0-9" ".")
+			name=$(printf "$f" | tr -c "A-Za-z0-9" ".")
 			sed -n \
 			-e "s/^URL: \(.*\)$/remote.$name.url \1 ./p" \
 			-e "s/^Pull: \(.*\)$/remote.$name.fetch \1 ^$ /p" \
diff --git a/git-quiltimport.sh b/git-quiltimport.sh
index 10135da..2ae1f20 100755
--- a/git-quiltimport.sh
+++ b/git-quiltimport.sh
@@ -89,7 +89,7 @@ for patch_name in $(cat "$QUILT_PATCHES/series" | grep -v '^#'); do
 			echo "No author found in $patch_name" >&2;
 			echo "---"
 			cat $tmp_msg
-			echo -n "Author: ";
+			printf "Author: ";
 			read patch_author
 
 			echo "$patch_author"
diff --git a/git-reset.sh b/git-reset.sh
index 91c7e6e..bf2a058 100755
--- a/git-reset.sh
+++ b/git-reset.sh
@@ -87,7 +87,7 @@ update_ref_status=$?
 case "$reset_type" in
 --hard )
 	test $update_ref_status = 0 && {
-		echo -n "HEAD is now at "
+		printf "HEAD is now at "
 		GIT_PAGER= git log --max-count=1 --pretty=oneline \
 			--abbrev-commit HEAD
 	}
diff --git a/t/t3800-mktag.sh b/t/t3800-mktag.sh
index 5b23b77..ede4d42 100755
--- a/t/t3800-mktag.sh
+++ b/t/t3800-mktag.sh
@@ -88,7 +88,7 @@ check_verify_failure '"type" line label check'
 #  5. type line eol check
 
 echo "object 779e9b33986b1c2670fff52c5067603117b3e895" >tag.sig
-echo -n "type tagsssssssssssssssssssssssssssssss" >>tag.sig
+printf "type tagsssssssssssssssssssssssssssssss" >>tag.sig
 
 cat >expect.pat <<EOF
 ^error: char48: .*"[\]n"$
diff --git a/t/t5401-update-hooks.sh b/t/t5401-update-hooks.sh
index cd8cee6..0514056 100755
--- a/t/t5401-update-hooks.sh
+++ b/t/t5401-update-hooks.sh
@@ -23,7 +23,7 @@ test_expect_success setup '
 cat >victim/.git/hooks/update <<'EOF'
 #!/bin/sh
 echo "$@" >$GIT_DIR/update.args
-read x; echo -n "$x" >$GIT_DIR/update.stdin
+read x; printf "$x" >$GIT_DIR/update.stdin
 echo STDOUT update
 echo STDERR update >&2
 EOF
@@ -32,7 +32,7 @@ chmod u+x victim/.git/hooks/update
 cat >victim/.git/hooks/post-update <<'EOF'
 #!/bin/sh
 echo "$@" >$GIT_DIR/post-update.args
-read x; echo -n "$x" >$GIT_DIR/post-update.stdin
+read x; printf "$x" >$GIT_DIR/post-update.stdin
 echo STDOUT post-update
 echo STDERR post-update >&2
 EOF
diff --git a/t/t6023-merge-file.sh b/t/t6023-merge-file.sh
index 1c21d8c..f3cd3db 100644
--- a/t/t6023-merge-file.sh
+++ b/t/t6023-merge-file.sh
@@ -52,7 +52,7 @@ super aquam refectionis educavit me;
 animam meam convertit,
 deduxit me super semitas jusitiae,
 EOF
-echo -n "propter nomen suum." >> new4.txt
+printf "propter nomen suum." >> new4.txt
 
 cp new1.txt test.txt
 test_expect_success "merge without conflict" \
-- 
1.5.0.rc1.gf4b6c
Junio C Hamano· Jan 16, 2007, 02:46 UTC · re: Jason Riedy · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

Jason Riedy <ejr@EECS.Berkeley.EDU> writes:
> Not all echos know -n.  This was causing a test failure in
> t5401-update-hooks.sh, but not t3800-mktag.sh for some reason.

We have done this already so it might be too late to raise this question, but does everybody have printf?

Show 10 quoted lines
> diff --git a/contrib/remotes2config.sh b/contrib/remotes2config.sh
> index 25901e2..b996996 100644
> --- a/contrib/remotes2config.sh
> +++ b/contrib/remotes2config.sh
> @@ -11,7 +11,7 @@ if [ -d "$GIT_DIR"/remotes ]; then
>  	{
>  		cd "$GIT_DIR"/remotes
>  		ls | while read f; do
> -			name=$(echo -n "$f" | tr -c "A-Za-z0-9" ".")
> +			name=$(printf "$f" | tr -c "A-Za-z0-9" ".")

This should almost be safe as no sane person would have a remote whose name is 'foo%s'...

Jason Riedy· Jan 16, 2007, 03:03 UTC · re: Junio C Hamano · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

And Junio C Hamano writes:
> We have done this already so it might be too late to raise this
> question, but does everybody have printf?
It's in the Single Unix Spec as the alternative to non-portable
echo uses:
  http://opengroup.org/onlinepubs/007908799/xcu/echo.html#tag_001_014_525
And no one has complained so far...
> > -			name=$(echo -n "$f" | tr -c "A-Za-z0-9" ".")
> > +			name=$(printf "$f" | tr -c "A-Za-z0-9" ".")
> This should almost be safe as no sane person would have a remote
> whose name is 'foo%s'...
Forgot about that, thanks.  It should be printf "%s" "$f".
Jason
Eric Blake· Jan 16, 2007, 03:09 UTC · re: Junio C Hamano · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
According to Junio C Hamano on 1/15/2007 7:46 PM:
Show 5 quoted lines
>> -			name=$(echo -n "$f" | tr -c "A-Za-z0-9" ".")
>> +			name=$(printf "$f" | tr -c "A-Za-z0-9" ".")
> 
> This should almost be safe as no sane person would have a remote
> whose name is 'foo%s'...

Why not go all the way, and make it safe, regardless of embedded %, embedded \, or leading -?

name=$(printf -- %s "$f" | tr -c "A-Za-z0-9" ".")
- --
Don't work too hard, make some time for fun as well!
Eric Blake             ebb9@byu.net
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (Cygwin)
Comment: Public key at home.comcast.net/~ericblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFFrEHw84KuGfSFAYARAlH9AJ9q3atCpSVBTg0SVhZ4DQ9T7qiqEQCfe9D2 bGTfwnvW6uN1ky6ncWYgkgc= =P9t5 -----END PGP SIGNATURE-----

Horst H. von Brand· Jan 17, 2007, 16:29 UTC · re: Junio C Hamano · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

Junio C Hamano <junkio@cox.net> wrote:
Show 6 quoted lines
> Jason Riedy <ejr@EECS.Berkeley.EDU> writes:
> > Not all echos know -n.  This was causing a test failure in
> > t5401-update-hooks.sh, but not t3800-mktag.sh for some reason.
> 
> We have done this already so it might be too late to raise this
> question, but does everybody have printf?
Here (Fedora rawhide) /usr/bin/printf is part of coreutils.
-- 
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                    Fono: +56 32 2654431
Universidad Tecnica Federico Santa Maria             +56 32 2654239
Casilla 110-V, Valparaiso, Chile               Fax:  +56 32 2797513
Junio C Hamano· Jan 17, 2007, 17:33 UTC · re: Horst H. von Brand · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

"Horst H. von Brand" <vonbrand@inf.utfsm.cl> writes:
Show 9 quoted lines
> Junio C Hamano <junkio@cox.net> wrote:
>> Jason Riedy <ejr@EECS.Berkeley.EDU> writes:
>> > Not all echos know -n.  This was causing a test failure in
>> > t5401-update-hooks.sh, but not t3800-mktag.sh for some reason.
>> 
>> We have done this already so it might be too late to raise this
>> question, but does everybody have printf?
>
> Here (Fedora rawhide) /usr/bin/printf is part of coreutils.

I am reasonably sure people on Linux land would be fine regardless of distros, but thanks anyway.

The problematic are less common ones, ranging from (not so less common) OSX, Solaris to (much less common) AIX that some people seem to run git on (or at least compile git for).

Brian Gernhardt· Jan 17, 2007, 18:00 UTC · re: Junio C Hamano · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

On Jan 17, 2007, at 12:33 PM, Junio C Hamano wrote:
> The problematic are less common ones, ranging from (not so less
> common) OSX, Solaris to (much less common) AIX that some people
> seem to run git on (or at least compile git for).

At least as far a OS X goes, it's installed on my machine (10.4.8) as /usr/bin/printf. I don't know if it's part of the Xcode packages, the "BSD Subsystem", or the core OS but I have a hard time imagining someone who didn't have them all installed using GIT.

Also, it's man page identifies itself as "PRINTF(1) BSD General Commands Manual PRINTF(1)" so most, if not all, flavors of BSD and BSD-like should be alright.

~~ Brian
Julian Phillips· Jan 17, 2007, 18:07 UTC · re: Junio C Hamano · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

On Wed, 17 Jan 2007, Junio C Hamano wrote:
Show 18 quoted lines
> "Horst H. von Brand" <vonbrand@inf.utfsm.cl> writes:
>
>> Junio C Hamano <junkio@cox.net> wrote:
>>> Jason Riedy <ejr@EECS.Berkeley.EDU> writes:
>>>> Not all echos know -n.  This was causing a test failure in
>>>> t5401-update-hooks.sh, but not t3800-mktag.sh for some reason.
>>>
>>> We have done this already so it might be too late to raise this
>>> question, but does everybody have printf?
>>
>> Here (Fedora rawhide) /usr/bin/printf is part of coreutils.
>
> I am reasonably sure people on Linux land would be fine
> regardless of distros, but thanks anyway.
>
> The problematic are less common ones, ranging from (not so less
> common) OSX, Solaris to (much less common) AIX that some people
> seem to run git on (or at least compile git for).
printf is a bash builtin ... not sure about other shells.
-- 
Julian

  ---
A lifetime isn't nearly long enough to figure out what it's all about.
Horst H. von Brand· Jan 17, 2007, 20:17 UTC · re: Julian Phillips · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

Julian Phillips <julian@quantumfyre.co.uk> wrote:
[...]
> printf is a bash builtin ... not sure about other shells.
It's a builtin in zsh and in ksh, and /not/ in tcsh here (Fedora rawhide)

zsh-4.2.6-3.fc7 ksh-20060214-1.1 tcsh-6.14-13

It might be special configuration, but as they are mentioned in the manpages I'd guess not.

-- 
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                    Fono: +56 32 2654431
Universidad Tecnica Federico Santa Maria             +56 32 2654239
Casilla 110-V, Valparaiso, Chile               Fax:  +56 32 2797513
Simon 'corecode' Schubert· Jan 18, 2007, 00:02 UTC · re: Junio C Hamano · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

Junio C Hamano wrote:
Show 5 quoted lines
>>> We have done this already so it might be too late to raise this
>>> question, but does everybody have printf?
> The problematic are less common ones, ranging from (not so less
> common) OSX, Solaris to (much less common) AIX that some people
> seem to run git on (or at least compile git for).
printf(1) is part of POSIX/SUSv3, so systems *should* have it.
cheers
  simon
-- 
Serve - BSD     +++  RENT this banner advert  +++    ASCII Ribbon   /"\
Work - Mac      +++  space for low €€€ NOW!1  +++      Campaign     \ /
Party Enjoy Relax   |   http://dragonflybsd.org      Against  HTML   \
Dude 2c 2 the max   !   http://golden-apple.biz       Mail + News   / \
Junio C Hamano· Jan 18, 2007, 00:13 UTC · re: Simon 'corecode' Schubert · lore

Re: [PATCH] Replace "echo -n" with printf in shell scripts.

Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:
Show 8 quoted lines
> Junio C Hamano wrote:
>>>> We have done this already so it might be too late to raise this
>>>> question, but does everybody have printf?
>> The problematic are less common ones, ranging from (not so less
>> common) OSX, Solaris to (much less common) AIX that some people
>> seem to run git on (or at least compile git for).
>
> printf(1) is part of POSIX/SUSv3, so systems *should* have it.

A lot of the portability problem we suffered were about the things that systems *should* have it.

Unfortunately a standard does not matter a whit on its own when dealing with the real world. That's why I muttered the above even though I knew POSIX says you should have one.

We use POSIX as one of the yardsticks to see what we should stay away (i.e. "that is not even in POSIX so let's not use it for now"), and also how far we are willing to bend over (i.e. "any reasonable system should have it; besides, it is in POSIX. Let's use it -- it is so convenient -- and insane platforms can screw themselves").

The key point in the latter is that "besides" is not "because".

← back to recent threads