echo -n bug
I discovered a very strange bug today with OS X’s Bourne shell. If you have OS X, give this a try:
/bin/sh -c 'echo -n bug'
This is what you should see:
$ /bin/sh -c 'echo -n bug'
bug$
This is what I see:
$ /bin/sh -c 'echo -n bug'
-n bug
$
In other words, it’s ignoring the -n option. It works fine in bash, it’s only sh that’s broken. It gets better though. If you’re using iTerm instead of Terminal.app, it works fine. I have combed through the environment, the locale settings, the terminal emulation, and I can’t account for it. I’ve tried ssh from a linux box which behaves the same as Terminal.app (broken). Who knows what black magic iTerm is invoking.
So I replaced my bash and sh with the ones from MacPorts:
sudo port install bash
sudo mv /bin/bash /bin/bash.old
sudo mv /bin/sh /bin/sh.old
sudo ln /opt/local/bin/bash /bin/bash
sudo ln /opt/local/bin/bash /bin/sh
While you’re at it, feel free to
sudo port install coreutils +default_names
Problem solved. Very odd, though. abcde uses echo -n heavily, which breaks in all sorts of ugly ways before this fix. This patch causes abcde to use /bin/bash instead of /bin/sh:
Index: abcde-2.3.3/abcde
===================================================================
--- abcde-2.3.3.orig/abcde 2007-12-07 18:46:36.000000000 -0700
+++ abcde-2.3.3/abcde 2007-12-07 20:57:17.000000000 -0700
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
# Copyright (c) 1998-2001 Robert Woodcock <rcw@debian.org>
# Copyright (c) 2003-2005 Jesus Climent <jesus.climent@hispalinux.es>
# This code is hereby licensed for public consumption under either the
Index: abcde-2.3.3/cddb-tool
===================================================================
--- abcde-2.3.3.orig/cddb-tool 2007-12-07 20:56:49.000000000 -0700
+++ abcde-2.3.3/cddb-tool 2007-12-07 20:57:19.000000000 -0700
@@ -1,4 +1,4 @@
-#!/bin/sh
+#!/bin/bash
# Copyright (C) 1999 Nathaniel Smith <njs@uclink4.berkeley.edu>
# Copyright (C) 1999, 2000, 2001 Robert Woodcock <rcw@debian.org>
Speaking of abcde, here’s a patch to get rid of an unrelated bug in 2.3.3:
Index: abcde-2.3.3/abcde
===================================================================
--- abcde-2.3.3.orig/abcde 2005-08-25 16:43:27.000000000 -0600
+++ abcde-2.3.3/abcde 2007-12-07 18:46:36.000000000 -0700
@@ -1946,7 +1946,7 @@
FILEPATH=$(find "$FILEPATH" | grep "/$REALTRACKNUM ");
# If the file exists, copy it
if [ -e "$FILEPATH" ] ; then
- nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG" $REDIR
+ nice $READNICE $CDROMREADER "$FILEPATH" "$FILEARG"
else
false
fi ;;
Finally I have abcde at my fingertips again. There’s no replacement for abcde when it comes to ripping CDs.
December 8th, 2007 at 01:29
It’s not a bug. It’s a feature :)
/bin/sh doesn’t have a -n option for echo. And if you symlink /bin/sh to bash and/or you invoke bash as /bin/sh (by, for example, prefacing your script with #!/bin/sh) it will behave like /bin/sh
A workaround would be to just use /bin/echo instead of the built-in command.
December 8th, 2007 at 07:03
Looks like you’re right, sort of. Here’s a page on echo behavior.
It looks like there’s no consensus on whether Bourne shell (not bash) has
echo -n, but I notice that it says the POSIX echo doesn’t.I thought it might be something like this at first, but the odd behavior in iTerm was a red herring. I’m reinstating the original
/bin/bashand/bin/sh, and calling it a bug in abcde.