[Ilugc] Error in Wiki page

  • From: binand@xxxxxxxxx (Binand Sethumadhavan)
  • Date: Sun Feb 6 19:40:06 2005

Hi All,

I was going through the Chennai LUG Wiki and found this page:

http://www.chennailug.org/wiki/FiveMinuteTipsBash4

The basic premise of the page is not correct. '[' is both an
executable AND a bash builtin, as seen here:

[root@avalanche root]# type -a [
[ is a shell builtin
[ is /usr/bin/[

It should be noted that '[' necessarily has to be a builtin at leash
on Redhat systems (like the one above) as it is used extensively in
startup scripts - scripts that cannot assume that a /usr partition is
mounted or available. An example is this:

if [ -f /.autofsck ]; then
   fsck -a -y -T
fi

At this point, the system has not mounted /usr - this is the "Your
system appears to have shut down uncleanly" handling code in Redhat's
init scripts (a clean shutdown does an rm -f /.autofsck).

In fact, quite a few of bash's builtins have /usr/bin or /bin
equivalents - like kill, test, true, false, pwd etc. But the semantics
of bash's command search ensures that the builtin version is used all
the time, unless the user specifies otherwise (using the "command
builtin :)

There are subtle differences in the way the builtins and commands
work. In this specific case, you can see this:

[root@avalanche root]# /usr/bin/[ -f /.autofsck; echo $?
0
[root@avalanche root]# [ -f /.autofsck; echo $?
-bash: [: missing `]'
2

(i.e., the builtin version demands a closing ']' whereas it is not
necessary for the command version).

Binand

Other related posts: