10
1
Not realising I had caps-lock on, I typed CD
into the command prompt on my mac. Surprisingly, it didn't error.
which CD
shows me /usr/bin/CD
. And if I examine that file, it looks like this:
#!/bin/sh
# $FreeBSD: src/usr.bin/alias/generic.sh,v 1.2 2005/10/24 22:32:19 cperciva Exp $
# This file is in the public domain.
builtin `echo ${0##*/} | tr \[:upper:] \[:lower:]` ${1+"$@"}
I guess it's something to do with translating upper to lower case, but ${0##*/}
is completely opaque to me. man CD
tells me "no manual entry". Can someone explain this command?
EDIT: Ok, I just noticed that OSX's filesystem is case-insensitive, so this file is actually /usr/bin/cd
. But if I type cd
normally, I of course get the builtin, so /usr/bin/cd
only gets called when I get my case wrong. I'd still like to know what it does.
Well, if that's the case, then it doesn't work! If I type, e.g.,
CD /
, it just does nothing. – John Fouhy – 2009-11-13T03:34:01.1801That's because it runs in a subshell; the subshell cd's to /, then exits, leaving your shell unmoved. – Gordon Davisson – 2009-11-14T07:37:10.613
As has been pointed out, this same script has a number of different filenames (hard links) - most if not all of them are UPPERCASE filenames. YES - in the case of CD it doesn't appear to work because cd has no effect when run in a subshell. That's why I wrote that it's "trying" to do the right thing. Indeed, it does not succeed. For SOME cases, this script will do the right thing; for example, if it's linked to LS, when you type "LS /USR/BIN" it will run "ls /usr/bin" – pbr – 2009-11-19T15:38:58.663
Also, those aren't regular expressions. They're Bash parameter expansions: http://www.gnu.org/software/bash/manual/bashref.html#Shell-Parameter-Expansion
– Johann – 2014-02-03T20:24:11.750