Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.
false - Return Unsuccessful Status
The false command is a shell builtin that always returns an unsuccessful exit status (1). It does nothing and produces no output, making it useful for scripting, conditional logic, and creating intentional failures.
Basic Syntax
false
Usage Examples
Basic Usage
# Always fails
false
# Check exit status
false && echo "This won't print" # Won't execute
false || echo "This will print" # Will print
# Use in conditional statements
if false; then
echo "This will never execute"
else
echo "This will always execute"
fi
Loop Control
# Loop that never executes
while false; do
echo "This will never run"
done
# Skip iteration with continue
for i in {1..10}; do
[ $((i % 2)) -eq 0 ] && false && continue
echo "Odd number: $i"
done
# Early loop termination
while true; do
check_condition
should_stop && false && break
process_data
done
Scripting Logic
# Conditional execution
function validate_input() {
[ -z "$1" ] && false && return 1
[ "$1" = "valid" ] || false && return 1
return 0
}
# Error simulation
function simulate_error() {
echo "Simulating an error condition"
false
echo "This won't be reached"
}
# Default failure case
function try_operations() {
operation1 && return 0
operation2 && return 0
operation3 && return 0
false # Default failure
}
Practical Examples
Error Handling
# Force error condition
function cleanup_on_error() {
echo "Error occurred, performing cleanup..."
cleanup_commands
}
# Chain of commands with error handling
command1 || {
echo "command1 failed"
false
} || {
echo "All attempts failed"
exit 1
}
# Testing error conditions
function test_error_handling() {
false && echo "Success path"
echo "This will still execute due to short-circuit"
}
Configuration and Feature Flags
# Disable features
function debug_mode() {
[ "$DEBUG" = "true" ] || false && return 1
echo "Debug: $1"
}
# Conditional execution based on config
function run_if_enabled() {
[ "$FEATURE_ENABLED" = "true" ] || false && return 1
"$@"
}
# Force disable in maintenance mode
function check_maintenance_mode() {
[ -f "/etc/maintenance" ] && false && {
echo "System under maintenance"
return 1
}
}
Validation and Testing
# Input validation
function validate_number() {
[[ "$1" =~ ^[0-9]+$ ]] || false && {
echo "Error: $1 is not a number"
return 1
}
}
# File existence validation
function require_file() {
[ -f "$1" ] || false && {
echo "Error: File $1 not found"
return 1
}
}
# Permission validation
function check_permissions() {
[ -r "$1" ] && [ -w "$1" ] || false && {
echo "Error: Insufficient permissions for $1"
return 1
}
}
System Administration
# Service status check
function is_service_running() {
systemctl is-active "$1" > /dev/null || false && {
echo "Service $1 is not running"
return 1
}
}
# Network connectivity test
function check_network() {
ping -c 1 8.8.8.8 > /dev/null || false && {
echo "Network connectivity issue"
return 1
}
}
# Disk space check
function check_disk_space() {
[ $(df / | tail -1 | awk '{print $5}' | sed 's/%//') -lt 90 ] || false && {
echo "Warning: Disk space below 10%"
return 1
}
}
Advanced Usage
Logic Gates Implementation
# NOT gate using false
function not() {
"$@" && false || true
}
# NAND gate
function nand() {
"$1" "$2" || false && return 1 || true
}
# NOR gate
function nor() {
"$1" "$2" && false || false
}
Conditional Compilation
# Feature toggles
function feature_enabled() {
case "$1" in
"feature_a") [ "$ENABLE_FEATURE_A" = "true" ] || false;;
"feature_b") [ "$ENABLE_FEATURE_B" = "true" ] || false;;
*) false;; # Default to disabled
esac
}
# Run feature if enabled
function run_feature() {
feature_enabled "$1" && "$2" || {
echo "Feature $1 is disabled"
false
}
}
Testing and Mocking
# Mock failing function
function mock_failure() {
echo "Simulating function failure"
false
}
# Test error handling paths
function test_error_scenarios() {
echo "Testing error handling..."
mock_failure || echo "Error handled correctly"
}
# Force failure for testing
function force_test_failure() {
false
echo "This shouldn't execute"
}
Performance Testing
# Benchmark false command performance
time for i in {1..1000000}; do false; done
# Compare with other failure methods
time false
time /bin/false
time exit 1
# Error handling overhead
start_time=$(date +%s%N)
for i in {1..100000}; do
false || true
done
end_time=$(date +%s%N)
echo "Error handling time: $((($end_time - $start_time) / 1000000))ms"
Debugging and Development
Conditional Debugging
# Debug switch that fails
function debug_check() {
[ "$DEBUG" = "true" ] && echo "Debug: $1" || false
return 0
}
# Verbose mode with failure path
function verbose_log() {
[ "$VERBOSE" = "true" ] && echo "$1" || false
}
# Development mode checks
function dev_mode_check() {
[ "$ENVIRONMENT" = "development" ] || false && {
echo "Production environment - development features disabled"
return 1
}
}
Error Simulation
# Simulate various error conditions
function simulate_error() {
case "$1" in
"network") echo "Network error"; false;;
"permission") echo "Permission denied"; false;;
"timeout") echo "Operation timeout"; false;;
*) echo "Unknown error"; false;;
esac
}
# Test robustness
function test_robustness() {
simulate_error "network" || echo "Network error handled"
simulate_error "permission" || echo "Permission error handled"
}
Best Practices
- Use
falsefor intentional failures in conditional logic - Combine with
||for error handling paths - Employ for input validation that should fail on invalid input
- Use in testing to simulate error conditions
- Leverage for feature disabling and configuration control
Security Considerations
# Secure default behavior
function secure_operation() {
[ "$SECURE_MODE" = "true" ] || false && {
echo "Operation not permitted in insecure mode"
return 1
}
# Perform secure operation
}
# Defensive programming
function validate_secure_input() {
[ "$#" -eq 1 ] || false && return 1
[[ "$1" =~ ^[a-zA-Z0-9_-]+$ ]] || false && return 1
return 0
}
Performance Characteristics
- Exit Status: Always returns 1 (failure)
- Output: No output produced
- Execution Time: Minimal overhead
- Resource Usage: Negligible
Related Commands
true- Return successful statusexit- Exit the shell with statusreturn- Return from functiontest- Evaluate conditional expression[(builtin)` - Conditional expression
Compatibility Notes
- Available in all POSIX-compliant shells
- Built-in command in bash, zsh, and most modern shells
- External
/bin/falsealso available on most systems - Consistent behavior across Unix-like systems
The false command is an essential tool in shell scripting for creating intentional failures, implementing conditional logic, and testing error handling paths. Its predictable behavior makes it invaluable for robust script development.