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

11 messages from 2007-01-16 to 2007-01-18. Participants: Jason Riedy, Junio C Hamano, Eric Blake, Horst H. von Brand, Brian Gernhardt, Julian Phillips, Simon 'corecode' Schubert.
Thread: https://gitlist.dev/t/6394

## Jason Riedy, 2007-01-16 01:31

Subject: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <17437.1168911089@lotus.CS.Berkeley.EDU>
URL: https://gitlist.dev/e/17437.1168911089%40lotus.CS.Berkeley.EDU

```
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(-)

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, 2007-01-16 02:46

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <7v1wlv1yeh.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v1wlv1yeh.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <17437.1168911089@lotus.CS.Berkeley.EDU>

```
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?

> 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, 2007-01-16 03:03

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <18039.1168916633@lotus.CS.Berkeley.EDU>
URL: https://gitlist.dev/e/18039.1168916633%40lotus.CS.Berkeley.EDU
In-Reply-To: <7v1wlv1yeh.fsf@assigned-by-dhcp.cox.net>

```
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, 2007-01-16 03:09

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <45AC41F0.8090106@byu.net>
URL: https://gitlist.dev/e/45AC41F0.8090106%40byu.net
In-Reply-To: <7v1wlv1yeh.fsf@assigned-by-dhcp.cox.net>

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

According to Junio C Hamano on 1/15/2007 7:46 PM:
>> -			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, 2007-01-17 16:29

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <200701171629.l0HGTCE3019292@laptop13.inf.utfsm.cl>
URL: https://gitlist.dev/e/200701171629.l0HGTCE3019292%40laptop13.inf.utfsm.cl
In-Reply-To: <7v1wlv1yeh.fsf@assigned-by-dhcp.cox.net>

```
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.
-- 
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, 2007-01-17 17:33

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <7vk5zlsglk.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7vk5zlsglk.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <200701171629.l0HGTCE3019292@laptop13.inf.utfsm.cl>

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

```

## Brian Gernhardt, 2007-01-17 18:00

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <F850C6F1-EFDC-4D37-90B4-27D8982BA086@silverinsanity.com>
URL: https://gitlist.dev/e/F850C6F1-EFDC-4D37-90B4-27D8982BA086%40silverinsanity.com
In-Reply-To: <7vk5zlsglk.fsf@assigned-by-dhcp.cox.net>

```

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, 2007-01-17 18:07

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <Pine.LNX.4.64.0701171804260.26185@reaper.quantumfyre.co.uk>
URL: https://gitlist.dev/e/Pine.LNX.4.64.0701171804260.26185%40reaper.quantumfyre.co.uk
In-Reply-To: <7vk5zlsglk.fsf@assigned-by-dhcp.cox.net>

```
On Wed, 17 Jan 2007, Junio C Hamano wrote:

> "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, 2007-01-17 20:17

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <200701172017.l0HKHIA8032440@laptop13.inf.utfsm.cl>
URL: https://gitlist.dev/e/200701172017.l0HKHIA8032440%40laptop13.inf.utfsm.cl
In-Reply-To: <Pine.LNX.4.64.0701171804260.26185@reaper.quantumfyre.co.uk>

```
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, 2007-01-18 00:02

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <45AEB932.3060509@fs.ei.tum.de>
URL: https://gitlist.dev/e/45AEB932.3060509%40fs.ei.tum.de
In-Reply-To: <7vk5zlsglk.fsf@assigned-by-dhcp.cox.net>

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

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, 2007-01-18 00:13

Subject: Re: [PATCH] Replace "echo -n" with printf in shell scripts.
Message-ID: <7v7ivlp4y0.fsf@assigned-by-dhcp.cox.net>
URL: https://gitlist.dev/e/7v7ivlp4y0.fsf%40assigned-by-dhcp.cox.net
In-Reply-To: <45AEB932.3060509@fs.ei.tum.de>

```
Simon 'corecode' Schubert <corecode@fs.ei.tum.de> writes:

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

```
