false Command in Linux



The false command in Linux returns an exit code 1 which indicates the failure. An exit code is a numerical value returned by a program or command to indicate the status of the execution. This command is useful in Bash scripting, especially for creating conditional expressions. It can also be used for loop control.

Note − The false command does not take any option or argument. It always returns exit code 1 and ignores any input provided.

Syntax of false Command

The syntax of the Linux false command is as follows −

false [arguments]

Any argument provided in the [arguments] field will be ignored.

Usage of false Command

This section demonstrates the usage of the false command in Linux with examples −

Basic Usage of false Command

Simply executing the false command does not display anything in the standard output −

false 
Basic Usage of false command 1

By default, the exit code is not displayed. To check the exit code, use −

echo $?

The $? gives the exit status code of the last executed command. The ? is a variable that stores the exit code.

Basic Usage of false command 2

The output shows the exit code 1.

The exit code 0 indicates the command executed without any error. While 2 indicates the misuse of shell built-ins.

Display Help

To display quick help related to a false command, use the man command −

man false

Using false Command in Bash Scripting

In bash scripting, the false command plays a vital role in building logic. It is primarily used with conditional statements and loop structures.

In the following example, the false command is employed with the conditional expression −

#!/bin/bash

echo "Starting Script!"

if false; then
   echo "This will not be printed."
else
   echo "The condition is false!"
fi

In the above script, the if false expression checks whether the condition is true. In this case, the false command will return a non-zero exit code indicating failure. So, the instructions in the else block will be printed.

false Command in Bash Scripting 1

Take another example. In this case, the counter will never update or print its value because the loop condition is always false.

#!/bin/bash

count=1

echo "Loop Starting!"

while false; do
   echo "Counter value: $count"
   count=$((count + 1))
done

echo "Loop Skipped."
false Command in Bash Scripting 2

The false command in the script can be used for debugging or troubleshooting errors. It essentially isolates a part of the script. This way other parts of the script can be debugged.

Deny Access to Shell Environment to a User

The false command can also be used to deny shell access to any user. For example, to deny shell access to a user john, first check which shell is assigned to this user, using the following command −

cat /etc/passwd | grep john
Deny Access to Shell Environment to User 1

The output shows that john is assigned a bash shell. To deny shell access to user john, open the /etc/passwd file in any editor −

sudo nano /etc/passwd

To modify this file, sudo privileges are needed.

Find the user john and change, its shell from /bin/sh to /bin/false. It will immediately stop any login attempt.

Deny Access to Shell Environment to User 2

Save the file and exit the editor.

Now, when the user (john) tries to log in, the shell will immediately terminate as shown in the following output image.

Deny Access to Shell Environment to User 3

Conclusion

The false command in Linux ignores any arguments given to it and executes without producing any output. It is primarily used in bash scripting with the true command. It is also used for testing and debugging the script.

In this tutorial, we explained the false command, its syntax, options, and usage in Linux through various examples.

Advertisements