The other day I came across Solaris’s implementation of /bin/true. I use “true” for infinite while loops quite often. However, I began to wonder what legitimate uses existed for “false”. Example: disabling shell access for user accounts. Do you know of another legitimate use of false? Submit an example by commenting on this post and win Chris F.A. Johnson’s Shell Scripting Recipes.

Rules:

  1. “Disabling shell access for user accounts” is not a valid submission.
  2. You cannot submit someone else’s submission.
  3. One submission per person.
  4. I will take submissions until March 9, 2008 10PM CST.
  5. One winner will be randomly chosen from the valid submissions.

28 Responses to “Win a book by submitting a legitimate use for /bin/false”

  1. Mike Says:

    #!/bin/sh
    /bin/false
    export TEST=$?
    while [ $TEST != 0 ]
    do
    /bin/true #replace with a program to keep doing until it works
    export TEST=$?
    done

  2. Janne Says:

    Generally, whenever you’re developing a shellscript, just temporarily replace real commands with /bin/false to easily test the error handling at different points without having to fake errors in some complicated and possibly faulty way.

  3. admin Says:

    Janne,

    Thats a good idea.

  4. Daniel Yokomizo Says:

    I use true and false in higher-order scripts. In a script I wrote (I don’t have it anymore, unfortunatly) I use different media conversion programs to convert different kinds of from and to media types, but sometimes the most specialized converter fails so I fall back to the less specialized (but more reliable) converter (also per media types). Instead of chaining dozens of if/else, I have a lookup table of conversions, if it isn’t there I return /bin/false, then I run the converter and if it fails I run the more reliable one. I use /bin/false as a monoid identity, so it let me have cleaner scrips (one to lookup the specialized and one to lookup the reliable) and in the main script I just compose them.

    I got this idea from functional programming and I used to do this all the time before I started using Haskell as a scripting language.

  5. eljunior Says:

    #! /bin/false
    # bota.sh
    # 25-02-2008

    # when you have a file that is meant to be
    # sourced by other scripts, and not be executed standalone# it’s a good idea to put /bin/false after the shabang

    echo -n “initializing something… ”
    CONF_VARIABLE=something
    echo “done”

  6. Niall Says:

    #!/bin/bash
    true=$(/bin/true)
    false=$(/bin/false)

    return $false

    return $true

  7. Matt Doar Says:

    Obviously /bin/false has to exist to preserve the balance of your OS. Otherwise it would be asymmetric and fall over.

  8. FatButtLarry Says:

    Matt Doar’s answer is hilarious.

    My best answer is this:

    Naturally, any script can need both “True” and “False”.

    If for some reason you did use “/bin/true”, and you derived the word “true” programmaticly (i.e: “/bin/$retval”), your script would break without an equally opposite script “/bin/false”.

    Why would you dynamically call your items in “/bin” ?

    Because you are a cowboy. And a cowboy wants his /bin folder to be /horse.

    #bin=”/bin” #Removed because I’m a cowboy.
    bin=”/horse” #Added because I’m a cowboy.

    In this event, you may find all of your program calls need to be precluded with $bin. Once you’re calling programs dynamically, you’ll find yourself doing the same with the second half. Lets say the application that is being called is $prg.

    call “$bin/$prg”

    This works great for “/horse/date”, “/horse/ls”, “/horse/true”, but if “true” was determined dynamically, you script will now break without the “false” equivalent.

    That may not be a good scenario of when to use it, but maybe it helps justify it’s existence.

    -Tres

  9. thecubic Says:

    actually did this today…

    #!/bin/bash

    if grep Fedora /etc/fedora-release; then
    true
    else
    # would return true/0 otherwise
    false
    fi

    the value of this isn’t apparent, but if you were to run this through ssh or some remote execution utility like that (assuming it sends rc), you could aggregate the results to determine (rc=0) Fedora and (rc=1) not Fedora.

    The other discussion point is that this simple-as-pie program does 31 system calls (Linux/x86_64) to achieve this goal.

  10. admin Says:

    It seems that “grep Fedora /etc/fedora-release” would work just as well? no?

  11. admin Says:

    Mike,

    Your right, but it should be noted that you get rid of the TEST var. Like so:

    #!/bin/sh
    /bin/false
    while [ $? != 0 ]
    do
    /bin/true #replace with a program to keep doing until it works
    done

  12. admin Says:

    FatButtLarry,

    I am speechless.

  13. pgas Says:

    Without false, until would be useless:

    until false; do
    : $((i++))
    sleep 1
    done

  14. pgas Says:

    I know the rules says only one entry, but ….you can use false to “comment” out a part of your script when debugging:

    if false; then
    .
    .
    .
    fi

  15. robert koch Says:

    You can use /bin/false in /etc/inetd.conf to replace a daemon like “finger” and just make some “jokes” on people who try to telnet the port managed by /bin/false.

    This can be used as a security alternative.
    You move your daemon to another non-standard port and use /bin/false as a “fake”.

  16. thecubic Says:

    you could rely on grep if you were aggregating, but if you wanted to actually do something based on that branch in the script itself (such as, try plan a - test for falseness, then try plan b). And also there’s some stuff that will fail but rc will be set to 0 - and sometimes different platforms or modes of use will have wildly different rc conditions.

  17. haxier Says:

    Hi!

    I use /bin/false as a fake login shell for some user accounts that don’t need to get a shell account, but need a identity in the LAN.

  18. admin Says:

    haxier,

    Sorry, this falls under rule number 1.

  19. Chris F.A. Johnson Says:

    Why would you use /bin/false (or /bin/true) in a script, when both are builtin shell commands?

  20. admin Says:

    Chris,

    I may not have made myself clear enough. Yes, I meant false generally, not just /bin/false. I said /bin/false so that people would know I meant a shell command / built-in function as opposed to the idea of false. Maybe I was over thinking?

  21. admin Says:

    I am guessing they are using /bin/false ^ there because thats what I mentioned?

  22. D Bennett Says:

    Reading “info false”: you should use /bin/false in preference to the shell builtin any time there is a chance your script will be run on an OS you can’t, or won’t, test the builtin with.

    FWIW, on my OSX host it is /usr/bin/false — so does not support the portability assumption info makes.

  23. Elias Pipping Says:

    To finally get rid of zero, one and magic numbers altogether ($1 was taboo, too):

    $ type zero
    zero is a function
    zero ()
    {
    true;
    echo $?
    }
    $ type succ
    succ is a function
    succ ()
    {
    echo $(($2+$(false; echo $?)))
    }
    $ echo $(succ - $(succ - $(succ - $(succ - $( zero )))))
    4

  24. D Bennett Says:

    /sbin/nash — the minimal shell your Linux probably starts up with — is not “false” aware. You see use of /bin/false in system startup scripts.

  25. Chris F.A. Johnson Says:

    “/sbin/nash &ndash the minimal shell your Linux probably starts up with – is not “false” aware.”

    But it surely has /bin (and /usr/bin) in its PATH.

    By using ‘false’ instead of ‘/bin/false’, you will use the shell’s builtin command if it exists, and the external command (wherever it is) when flase is not builtin.

    If you use /bin/false, your script is more likely to fail, and will usually (or certainly, if you are using bash) be less efficient.

  26. harry Says:

    I don’t get it

  27. otheus Says:

    Though I saw some very good ideas above, the way I’ve used it the most is to defer the error status, so that you can, say, print a warning message inside a script, function, or make operation, and still allow the rest of the script to “see” the failed operation.

    For instance, in a makefile, you might have:

    target : foo.c
    uname | grep Linux >/dev/null || \
    { echo >&2 “You need to make this with Linux!”; false }
    … other make rules …

    (N.B.: there are better ways to handle architecture-specific compilation.)

    Or to replace a built-in function with one of your own design:

    make() {
    command make “$@” 2>/tmp/.make.$USER.$$ || {
    less -E /tmp/.make.$USER.$$
    rm /tmp/.make.$USER.$$
    false
    }
    }

    Yeah, theoretically, you can capture $? and save it to a variable and then use it in a return. Sometimes, however, you want to inline the whole thing, and there’s nothing to return to.

  28. MERLiiN Says:

    I use both true and false in daemon init scripts to ensure that the false I return on failures match what the OS expect. Just because it isn’t sane doesn’t stop someone from re-writing their linux to expect -59 for true and 128 for false. That’s the beauty of open source, and I see too many init scripts rely on “exit 2″ for failures. As most of thise scripts are vendor supplied one can assume that they are correct, and you know what they say about “assume” don’t you.

    On a sidenote, you should use /sbin/nologin over /bin/false to disable accounts these days. It’s the “correct” way of doing it. Use man nologin for the details.

Leave a Reply

If Wordpress eats your comment (shell output, loops, ex..) email the text to me.