Win a book by submitting a legitimate use for /bin/false
February 24th, 2008

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:
- “Disabling shell access for user accounts” is not a valid submission.
- You cannot submit someone else’s submission.
- One submission per person.
- I will take submissions until March 9, 2008 10PM CST.
- One winner will be randomly chosen from the valid submissions.


February 24th, 2008 at 5:53 pm
#!/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
February 24th, 2008 at 7:39 pm
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.
February 24th, 2008 at 7:49 pm
Janne,
Thats a good idea.
February 24th, 2008 at 9:18 pm
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.
February 24th, 2008 at 11:16 pm
#! /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”
February 25th, 2008 at 7:50 am
#!/bin/bash
true=$(/bin/true)
false=$(/bin/false)
…
return $false
…
return $true
February 25th, 2008 at 1:15 pm
Obviously /bin/false has to exist to preserve the balance of your OS. Otherwise it would be asymmetric and fall over.
February 25th, 2008 at 1:52 pm
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
February 25th, 2008 at 8:40 pm
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.
February 25th, 2008 at 8:54 pm
It seems that “grep Fedora /etc/fedora-release” would work just as well? no?
February 25th, 2008 at 9:39 pm
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
February 25th, 2008 at 9:40 pm
FatButtLarry,
I am speechless.
February 26th, 2008 at 8:05 am
Without false, until would be useless:
until false; do
: $((i++))
sleep 1
done
February 26th, 2008 at 1:07 pm
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
February 26th, 2008 at 4:42 pm
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”.
February 26th, 2008 at 8:38 pm
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.
February 27th, 2008 at 11:24 am
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.
February 27th, 2008 at 11:28 am
haxier,
Sorry, this falls under rule number 1.
February 27th, 2008 at 10:51 pm
Why would you use /bin/false (or /bin/true) in a script, when both are builtin shell commands?
February 27th, 2008 at 10:57 pm
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?
February 27th, 2008 at 10:58 pm
I am guessing they are using /bin/false ^ there because thats what I mentioned?
February 28th, 2008 at 9:45 am
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.
February 28th, 2008 at 2:25 pm
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
February 29th, 2008 at 9:38 am
/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.
March 1st, 2008 at 6:25 pm
“/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.
March 4th, 2008 at 4:40 pm
I don’t get it
March 8th, 2008 at 4:18 pm
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.
March 9th, 2008 at 7:50 am
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.