Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux false Command

Linux false Command

Linux Command Reference Linux Command Reference


I. Overview of the false command

false is one of the simplest commands in Linux/Unix systems; its only function is to return a non-zero exit status code (usually 1), indicating that the command execution "failed".

Basic characteristics


II. Syntax of the false command

Basic syntax



false [选项]

Command options

The false command actually has no available options (by design), but for consistency it accepts the following standard options:

Option Description
--help Display help information (supported in the GNU version)
--version Display version information (supported in the GNU version)

Note: in most basic implementations, these options are ignored and 1 is still returned


III. How the false command works

Execution flow

Example

graph TD

    A[call false command] --> B[immediately set exit code to 1]

    B --> C[return to shell]

System implementation

On most systems, false exists both as a shell built-in command and as a standalone program:

  1. Built-in command: implemented directly by the shell, with the fastest execution
  2. Standalone program: usually located at /bin/false or /usr/bin/false

You can use the type command to view the specific implementation:

Example

type

false

Possible output:

false is a shell builtin

or

false is /bin/false


IV. Typical uses of the false command

1. Placeholder in scripts

Example

Use false temporarily as a placeholder for a function yet to be implemented

function

feature_to_be_implemented

(

)

{

   

false

 

Placeholder: actual implementation to be added later

}

2. Force an error status

Example

Force the script to exit if a condition is not met

check_dependencies

(

)

{

   

[

-f

"/path/to/required/file"

]

||

false

}

3. Infinite loop control

Example

Combine with while to create an infinite loop

while

false

;

do

   

echo

"This line will never execute"

done

In practice, while true is more commonly used; here we demonstrate the use of false

4. Test error handling

Example

Test the error-handling logic of a script

false

&&

{

   

echo

"This line will not execute because false returns non-zero"

}

false

||

{

   

echo

"This line will execute because false returns non-zero"

}


V. Comparison of false with related commands

Command Exit code Main Use Perform the operation
false 1 Indicate failure None
true 0 Indicate success None
: (colon) 0 No operation None
exit 1 1 Exit the script Terminate the current shell

VI. Practical exercises

Exercise 1: Observe the exit code

Example

false

echo

$?

 

will output 1

Exercise 2: Conditional test

Example

if

false

;

then

   

echo

"will not execute"

else

   

echo

"will execute"

fi

Exercise 3: Combine with logical operators

Example

false

&&

echo

"will not be displayed"

false

||

echo

"will be displayed"

!

false

&&

echo

"will be displayed"

Exercise 4: Use in a function

Example

test_user

(

)

{

   

[

"$(whoami)"

=

"root"

]

||

false

}

test_user

&&

echo

"You are root"

||

echo

"You are not root"


VII. Notes and precautions

  1. Performance considerations: although false consumes almost no resources, frequently calling the external false command (non-built-in) in a high-performance loop still incurs a small overhead
  2. Readability: in scripts, sometimes using return 1 is more explicit than false
  3. Compatibility: all Unix-like systems support false, but some embedded systems may only have a built-in implementation
  4. Debugging tips: you can use set -x to view the actual execution of false

VIII. Extended knowledge

1. Special uses of /bin/false

Systems often use /bin/false as the login shell for certain service accounts, prohibiting them from logging in:

Example

in /etc/passwd

nobody:x:

65534

:

65534

:nobody:

/

nonexistent:

/

bin

/

false

2. Implementation principle

The source code of the false command in GNU coreutils is extremely simple:

Example

int

main

(

void

)

{

   

return

EXIT_FAILURE

;

 

// usually defined as 1

}

3. Historical background

The false command first appeared in Version 7 Unix in 1979, serving as a basic building block of shell programming together with true.


Linux Command Reference Linux Command Reference