10.5: Fix the echo -n problem in 10.5

Nov 20, '07 02:30:00PM

Contributed by: randalla

Note: This hint is not correct. However, there's good information in the comments about why, along with workarounds. Please do not implement the hint, but if you're looking for alternatives, check the comments.
After much frustration, it seems that I have found a bug in 10.5. When doing unix scripting, it's useful to be able to do something like the following:
#!/bin/sh
echo -n "Insert Name: "
read NAME
echo "Hello ${NAME}!"
However, when using /bin/sh as the shell, echo -n no longer works. This is pretty much the most basic of basics in the unix world, and it just irks me that it's giving me grief. You can, of course, fix it by changing your shell to bash instead of sh, ala:
#!/bin/bash
Unfortunately, that only affects you. If there are other scripts you are running that you don't want to modify yourself, then you need to do something more drastic.

Because OS X uses bash for both /bin/sh and /bin/bash, it seems reasonable that they should be the same. It turns out that they aren't:

$ ls -l /bin/sh /bin/bash
-rwxr-xr-x  1 root  wheel  1244912 Sep 23 18:41 /bin/bash
-r-xr-xr-x  1 root  wheel  1244944 Sep 23 18:45 /bin/sh
Notice that bash is 32 bytes off of sh here. Both, however, return that they are bash:
$ /bin/sh --version
GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)
Copyright (C) 2005 Free Software Foundation, Inc.

$ /bin/bash --version
GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)
Copyright (C) 2005 Free Software Foundation, Inc.
So, since /bin/bash works, and /bin/sh doesn't, and the binaries are different, you can fix all this by just renaming /bin/sh to something else, and then copying /bin/bash to /bin/sh (before doing any of this, make appropriate backups):
$ sudo mv /bin/sh /bin/sh.orig
$ sudo cp /bin/bash /bin/sh
You should now have a working echo -n from here on. Hopefully they will fix this in a later release of 10.5.

[robg adds: I can confirm this still seems broken in 10.5.1.]

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20071106192548833