[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Worth mentioning in documentation

From: Bob Proulx
Subject: Re: Worth mentioning in documentation
Date: Mon, 10 Aug 2015 14:02:34 -0600
User-agent: Mutt/1.5.23 (2014-03-12)

Greg Wooledge wrote:
> Juanma wrote:
> > > [ is an ordinary command (a "shell builtin")
> > 
> > Here is another point I find confusing: I thought a "shell builtin" didn't
> > have a separate binary executable file, like 'cd' (which cd => fail), but
> > some of them do have such form (which [ => /usr/bin/[ ; which pwd =>
> > /bin/pwd). I also fail to see how 'test' modifies the state of the shell
> > itself (like 'cd' does), or why it is "impossible or inconvenient to obtain
> > [its functionality] with separate utilities".
> Don't use which(1).  Which is an external program, so it has no knowledge
> of the shell's builtins, aliases, functions and keywords.  Instead, use
> type.

Another problem with 'which' is that it was originally designed and
written for csh users specifically.  It was a csh script and would
source the users ~/.cshrc file so as to acquire their aliases from
there so as to be able to report user aliases.  I think it is still
that way on HP-UX (and probably others) for example.

However that obviously won't work well for bash, ksh, zsh, and other
shell users.  And IMNHO csh is a terrible shell regardless of the tcsh
users using it who think otherwise.  Therefore some distributions such
as Debian have rewritten 'which' as a /bin/sh script meaning that
'which' behaves differently on different systems.  It is non-portable.

> imadev:~$ type cd
> cd is a shell builtin
> imadev:~$ type [[
> [[ is a shell keyword
> imadev:~$ type -a test
> test is a shell builtin
> test is /usr/bin/test
> test is /bin/test
> Bash implements test as a builtin not because it's necessary, but simply
> for efficiency.  Forking a whole process to test whether two strings are
> equal would be horribly wasteful.

An important mental concept is that test and [ must *behave* the same
as if they were an external program.  They are internal builtins for
efficiency now but everything behaves the same if they are external
programs.  This is why quoting as if they were external programs is
required.  On the other hand [[ has always been a builtin and
therefore the shell can avoid one layer of quoting and does.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]