Pick of the Week - Nov 10 [Show all picks]
Path Finder 5 - A feature-laden Finder replacement
Submit Hint Search The Forums LinksStatsPollsFAQHeadlinesRSS
12,000 hints and counting!

10.5: Fix the echo -n problem in 10.5 UNIX
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.]
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[5,274 views]  

10.5: Fix the echo -n problem in 10.5 | 13 comments | Create New Account
Click here to return to the '10.5: Fix the echo -n problem in 10.5' hint
The following comments are owned by whomever posted them. This site is not responsible for what they say.
10.5: Fix the echo -n problem in 10.5
Authored by: antifuchs on Tue, Nov 20 2007 at 2:52PM PST
This is not a bug. As OS X 10.5 is now certified to follow the Single Unix Specification, its /bin/sh and echo builtin now follow the specification: http://www.opengroup.org/onlinepubs/007908799/xcu/echo.html

The -n switch is a bash add-on, so if you want to use this in your shell scripts, it is advisable to fix these shell scripts to use #!/bin/bash, and not to replace the strictly standards-compliant /bin/sh in OS X with the one that's not.

[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: hypert on Mon, Nov 26 2007 at 9:28AM PST
Note the other item mentioned on that page too:
New applications are encouraged to use printf instead of echo.
I had read something like that years ago and switched to printf (and sometimes just plain "print" for most of my shell scripts (usu. /bin/sh in Solaris 2.8)).

[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: cran on Tue, Nov 20 2007 at 2:53PM PST
It still works if you call it as /bin/echo in your script.

[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: bryanl on Tue, Nov 20 2007 at 2:53PM PST

Why don't you just use /bin/echo? It supports the -n flag. Seems a bit safer than moving files around.

bryanl http://smartic.us

[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: mluger on Tue, Nov 20 2007 at 3:00PM PST
1) It is very stupid to change the shells like this, it could break system scripts..


2) If you just did a 'man echo' you could have read that you can use this:

echo "Insert name:\c"

I quote:

"The following option is available:

-n Do not print the trailing newline character. This may also
be achieved by appending `\c' to the end of the string."

"Some shells may provide a builtin echo command which is similar or
identical to this utility. Most notably, the builtin echo in sh(1)
does not accept the -n option. Consult the builtin(1) manual page."


3) If you want another shell. Ctrl-click on your loginname at the account settings in the Sytem Preferences. This give you access to 'Advanced options'. There you can change your login shell.



[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: ebaur on Tue, Nov 20 2007 at 3:13PM PST
Another person echoing the objections here... (sorry about the pun there).

Instead of moving things around (an potentially causing issues with other scripts, system or third party), you should just change the #! line in your script.

If you want bash to run, use #!/bin/bash instead of #!/bin/sh - that's all. That's the purpose of the #! line.

[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: ulfda on Tue, Nov 20 2007 at 3:20PM PST
As far as I can remember (I've used Unix since around 1985), the built-in echo in sh has never accepted the -n flag. So, I don't think this is a bug.

[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: ducasi on Tue, Nov 20 2007 at 4:05PM PST
I've used Unix for about the same length of time.

The "echo -n" convention was in 4.2BSD, and so all BSD-derived unices supported this, including FreeBSD, where much of the unix utilities in Mac OS X comes from.

The "echo \c" style was from SYSV Unix, which was the main commercial stream of Unix.

There was a coming together of the two systems around SYSV revision 4 – "SVR4", when with Sun's help AT&T merged in missing features from BSD into SYSV. Unfortunately the "-n" flag to echo didn't make it. Probably because the flags like "\c" that the SYSV echo understands are more powerful than just the "-n" flag, and it also muddies the semantics of the command.

The so-called "Single Unix Specification" was based upon SVR4, so it codifies the "\c" style, and thus that is what Apple needed to support to pass the test, and officially have OS X be called "Unix".



[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: dewab on Tue, Nov 20 2007 at 3:28PM PST
Another option, if you don't want to edit the magic line (the #!/bin/shell line) is to explicitly specify the shell to use to run the script.

I.e.

as opposed to:
$ ./script.sh

do a:
$ /usr/bin/bash ./script.sh

Bit of a pain, but you can avoid editing files if that's a concern.

[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: unixhead94 on Tue, Nov 20 2007 at 3:56PM PST

This is a feature, not a bug.

For all we know, there may very well be some old BSD cruft that depends on /bin/sh behavior that may break if you do this.

It's surprising that this fix even works since bash is supposed to switch to POSIX mode if it's invoked as /bin/sh.

http://www.gnu.org/software/bash/manual/bashref.html#SEC85



[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: jeremyp on Tue, Nov 20 2007 at 4:12PM PST
The bash man page I consulted said that invoking it as sh emulates sh start up behaviour (i.e. ignores .bash_profile and .bashrc) but it does not state anywhere that builtin commands will revert to sh behaviour.

In fact, I tried echo -n from bash and sh on my 10.4 system and my Linux system and in all cases, the -n was recognised as a switch. On both of those machines, /bin/sh and /bin/bash were identical (in fact, on the Linux box, /bin/sh was a symlink).

It looks to me like Apple have compiled bash with a patch to produce sh on 10.5. It may be that the patch only disables the echo -n switch, but it may be that it alters the behaviour of sh in other ways in order to be Unix compliant. For this reason, I'd say it is quite dangerous to replace /bin/sh with /bin/bash, random shell scripts might break as a result.

In fact, I'd agree with the opinion expressed earlier that the 10.5 behaviour amounts to a bug fix.

[ Reply to This | # ]
bash from MacPorts
Authored by: corienti on Wed, Nov 21 2007 at 2:54PM PST
I was frustrated at this immediately as soon as I installed Leopard.

My solution was simply to install bash from MacPorts (which I was using in Tiger anyway) and then use dscl to change all user acccounts (including root) to use /opt/local/bin/bash for UserShell.

So it was fixed on the first day for me :-)

NB I quite agree, never replace /bin/sh with /bin/bash, that kind of thing is rather bad practice. Update your UserShell to use /bin/bash if you wish.

I always write my scripts with #!/bin/bash anyway, never /bin/sh, so scripts weren't a problem, only login sessions.


[ Reply to This | # ]
10.5: Fix the echo -n problem in 10.5
Authored by: g16l on Wed, Dec 12 2007 at 7:41AM PST
This is a feature, not a bug:
see "COMMAND_MODE environment variable" in
[link:]http://developer.apple.com/releasenotes/Darwin/RN-Unix03Conformance/



[ Reply to This | # ]