Discussion:
basename -bash generates an error
Brian Matheson
2007-04-19 02:34:09 UTC
Permalink
Hi there,

I occasionally use basename $0 in scripts that I sometime source in my
interactive shell. This construct seems to be broken now.

Old behavior:
red:~$ basename $0
-bash

New behavior:
orange:~/lib/sh$ basename $0
basename: invalid option -- b
Try `basename --help' for more information.

Looks like a misbehavior in option parsing.

Randomly,
Brian
Eric Blake
2007-04-19 03:06:59 UTC
Permalink
Post by Brian Matheson
red:~$ basename $0
-bash
Broken.
Post by Brian Matheson
orange:~/lib/sh$ basename $0
basename: invalid option -- b
Try `basename --help' for more information.
Correct behavior, but undesired results.

So what you really wanted to do:

basename -- "$0"

This correctly handles the case when $0 contains shell metacharacters, as
well as when it starts with -.
Post by Brian Matheson
Looks like a misbehavior in option parsing.
No, just a misunderstanding on your part. POSIX requires the current
behavior.

- --
Don't work too hard, make some time for fun as well!

Eric Blake ***@byu.net
Brian Matheson
2007-04-19 03:22:54 UTC
Permalink
Thanks guys, you learn something new every day.

Cheers,
Brian
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Post by Brian Matheson
red:~$ basename $0
-bash
Broken.
Post by Brian Matheson
orange:~/lib/sh$ basename $0
basename: invalid option -- b
Try `basename --help' for more information.
Correct behavior, but undesired results.
basename -- "$0"
This correctly handles the case when $0 contains shell metacharacters, as
well as when it starts with -.
Post by Brian Matheson
Looks like a misbehavior in option parsing.
No, just a misunderstanding on your part. POSIX requires the current
behavior.
- --
Don't work too hard, make some time for fun as well!
-----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
iD8DBQFGJtzR84KuGfSFAYARAi9SAKC1lELw/+J6mLYBQ8hyIRcOAwWn9ACg1apm
cs2wFv7zfGaZDV95JD6nxYc=
=79ow
-----END PGP SIGNATURE-----
Bob Proulx
2007-04-19 03:11:48 UTC
Permalink
Post by Brian Matheson
I occasionally use basename $0 in scripts that I sometime source in my
interactive shell. This construct seems to be broken now.
red:~$ basename $0
-bash
orange:~/lib/sh$ basename $0
basename: invalid option -- b
Try `basename --help' for more information.
Looks like a misbehavior in option parsing.
Thank you for your report. However this was an intentional change.
Documentation for intentional changes can be found in the NEWS file.
Here is the news concerning your issue.

* Major changes in release 5.3.0 (2005-01-08)

The following commands now reject unknown options instead of
accepting them as operands, so that users are properly warned that
options may be added later. Formerly they accepted unknown options
as operands; e.g., "basename -a a" acted like "basename -- -a a".

basename dirname factor hostname link nohup sync unlink yes

To ensure correct operation for both old and new versions of the
command it is necessary to indicate the end of options using "--".
For example the following will do what you want.

basename -- $0

That may similarly be applied to other commands to differentiate
options from arguments that may unintentionally start with an option
start character.

Bob
Loading...