Skip to contentSkip to conversation

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

PATCH In review Jason Riedy 8 participants 11 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Replace "echo -n" with printf in shell scripts.

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

---
 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(-)
contrib/remotes2config.sh +1 -1
index 25901e2..b996996 100644
@@ -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" \
git-quiltimport.sh +1 -1
index 10135da..2ae1f20 100755
@@ -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"
git-reset.sh +1 -1
index 91c7e6e..bf2a058 100755
@@ -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
 	}
t/t3800-mktag.sh +1 -1
index 5b23b77..ede4d42 100755
@@ -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"$
t/t5401-update-hooks.sh +2 -2
index cd8cee6..0514056 100755
@@ -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
t/t6023-merge-file.sh +1 -1
index 1c21d8c..f3cd3db 100644
@@ -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" \
Signature · 1 line
1.5.0.rc1.gf4b6c
#1 lore raw
Quoted 2 lines · Jason Riedy

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?

contrib/remotes2config.sh if [ -d "$GIT_DIR"/remotes ]; then · line 14

Quoted 10 lines
contrib/remotes2config.sh +1 -1
index 25901e2..b996996 100644
@@ -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'...

#2 lore raw
Quoted 2 lines · And Junio C Hamano

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

Quoted 5 lines

contrib/remotes2config.sh if [ -d "$GIT_DIR"/remotes ]; then · line 14

Quoted 2 lines
contrib/remotes2config.sh +1 -1
-			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

#3 lore raw
In reply to Junio C Hamano
Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Jason Riedy

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.

Signature · 4 lines
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
#4 lore raw
Quoted 9 lines · Horst H. von Brand
Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Jason Riedy

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

#5 lore raw
Quoted 5 lines · Junio C Hamano
Quoted 2 lines
Quoted 2 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

Signature · 4 lines
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   / \
#6 lore raw
Quoted 8 lines · Simon 'corecode' Schubert
Quoted 5 lines · Junio C Hamano
Quoted 2 lines
Quoted 2 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.

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

#7 lore raw
In reply to Junio C Hamano
Quoted 3 lines · Junio C Hamano

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

#8 lore raw
In reply to Junio C Hamano
Quoted 10 lines · Junio C Hamano
Quoted 8 lines · Horst H. von Brand
Quoted 6 lines · Junio C Hamano
Quoted 2 lines · Jason Riedy

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?

Quoted 8 lines
Quoted 1 line

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.

Signature · 4 lines
Julian

 ---
A lifetime isn't nearly long enough to figure out what it's all about.
#9 lore raw

Julian Phillips <[email protected]> wrote:

[...]

Quoted 1 line

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.

Signature · 4 lines
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
#10 lore raw
In reply to Junio C Hamano

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

According to Junio C Hamano on 1/15/2007 7:46 PM:

Quoted 5 lines

contrib/remotes2config.sh if [ -d "$GIT_DIR"/remotes ]; then · line 14

Quoted 2 lines
contrib/remotes2config.sh +1 -1
-			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 [email protected]
-----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-----