1

On macOS 10.14, using whatis on a command that is not installed prints 'command': nothing appropriate as is expected. But returns 0 instead of an error code, which I believe is expected behavior. It is also not consistent with man, which returns 1 after no results. Additionally, whatis prints to stdout, while man prints to stderr.

This is different than Linux, which returns an error code, and prints to stderr.

macOS:

$ whatis abc; echo $?
abc: nothing appropriate
0

$ man abc; echo $?
No manual entry for abc
1

$ whatis abc 2>/dev/null
abc: nothing appropriate

$ man abc 2>/dev/null

Linux (Ubuntu):

$ whatis abc; echo $?
abc: nothing appropriate.
16

$ man abc; echo $?
No manual entry for abc
16

$ whatis abc 2>/dev/null

$ man abc 2>/dev/null

I believe whatis should not return 0 when it fails to find a description of the command, and should have behavior consistent with man

Note: I noticed this because I'm writing a program that relies on this functionality

Edit: apropos has the same behavior as whatis

2

On macOS whatis is just a shell script.

$ type whatis
whatis is hashed (/usr/bin/whatis)
$ file /usr/bin/whatis
/usr/bin/whatis: POSIX shell script text executable, ASCII text

If you look inside, the relevant part reads

while [ "$1" != "" ]
do
    found=0
    for d in /var/cache/man $manpath /usr/lib
    do
        if [ -f $d/whatis ]
        then
            if grep -"$grepopt1" "$grepopt2""$1" $d/whatis
            then
                found=1
            fi
        fi
    done

    if [ $found = 0 ]
    then
        echo "$1: nothing appropriate"
    fi

    shift
done | eval ${PAGER:-more -E}

So if no entries are found (the if [ $found = 0 ] part), the message is displayed without setting any exit code. Actually the script only exits with status 1 if wrong arguments have been passed.

The most recent update notice relates to 2003-08-01 as the date of update, so it seems to be rather old anyway. Whether the behaviour is a feature or a bug is open for discussion.


In case you are wondering about apropos showing the same behaviour: apropos and whatis are basically the same script.

$ ll /usr/bin/{apropos,whatis}
-r-xr-xr-x  1 root  wheel  1808 Aug 18 00:18 /usr/bin/apropos*
-r-xr-xr-x  1 root  wheel  1806 Aug 18 00:18 /usr/bin/whatis*
$ diff /usr/bin/{apropos,whatis}
26,27c26,27
< grepopt1=$aproposgrepopt1
< grepopt2=$aproposgrepopt2
---
> grepopt1=$whatisgrepopt1
> grepopt2=$whatisgrepopt2
  • Thank you. In Ubuntu whatis is compiled, so we cannot do a similar analysis on it. I would argue that macOS' behavior is inferior to Linux's. It seems clear that trying to get information about a command that does not exist should return an error. which abc returns 1, man abc returns 1, etc. While whatis is not part of POSIX, I cannot say Apple's implementation is 'wrong', but I cannot see advantages to returning 0 and outputting to stdout. – Max Coplan Jan 18 at 17:57
  • 1
    @MaxCoplan Probably easier to roll your own version if needed. – nohillside Jan 18 at 18:00
  • 1
    That's what I've just done. Thank you! Your answer let me simply copy whatis to my local bin and edit it there. – Max Coplan Jan 18 at 18:50
  • I have posted a follow-up question to apple.stackexchange.com/questions/349920/… – Max Coplan Jan 28 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .