Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This is a textbook example of a bikeshed: http://bikeshed.com

Nobody is calling true or false in a script with options/arguments. Nobody hss ever seen true/false fail because they crashed in setlocale. Nobody's highest, second-highest, or 1000th-highest risk to their code is the performance of their true/false implementation.

But it's a really easy thing to have opinions about. You use UNIX for one week and suddenly you're exactly as qualified as Dennis Ritchie to have opinions on the matter.



I've seen... (ok, actually I recommended)... someone do the following:

    if [ -x /usr/bin/blah ]
    then
       BLAH=/usr/bin/blah
    else
       BLAH=/bin/true
    fi

    $BLAH foo bar baz
They wanted BLAH to be a noop if blah didn't exist. I could only think of /bin/true as a noop at the time, but I needed it to ignore parameters.

The man page defines that it does.

(in retrospect, we could have defined an sh function noop {} )


I was going to mention that it came up just days ago on Unix and Linux StackExchange. (-:

* https://unix.stackexchange.com/a/497561/5132

The conditionally defined shell function in one of the other answers is a more appealing approach, especially given the native behaviours of some shells with respect to variable expansion. But also given that /bin/true is not necessarily the pathname.

    % export BLAH='/bin/true too'
    % dash -c '$BLAH 1 2 3'
    % dash: 1: /bin/true: not found
    % sh -c '$BLAH 1 2 3'
    /bin/true: not found
    % ksh -c '$BLAH 1 2 3'
    ksh: /bin/true: not found
    % zsh -c '$BLAH 1 2 3'
    zsh:1: no such file or directory: /bin/true too
    % bash -c '$BLAH 1 2 3'
    bash: /bin/true: No such file or directory
    % 
    % export BLAH='/usr/bin/true too'
    % zsh -c '$BLAH 1 2 3'
    zsh:1: no such file or directory: /usr/bin/true too
    % bash -c '$BLAH 1 2 3'
    % ksh -c '$BLAH 1 2 3'
    % dash -c '$BLAH 1 2 3'
    %


Yeah, that was my answer -- and actually I wrote /usr/bin/true on SO but got sloppy here.

I wrote that answer because I recalled it as a common idiom in autoconf or something -- some automake system I'd seen where the system seeks out the tools it needs and defines them as variables.


Yeah, options include a noop function (which is nice because you can change it to noop () { echo "Skipping $@" >&2 ; }) or the shell builtin :, which definitely ignores its arguments.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: