Linux command
else 命令
文件
涉及管道、覆盖或删除,执行前请先确认路径和参数。
常用示例
Use else in
if [condition]; then echo "true"; else echo "false"; fi
Else after elif
if [ $x -lt 0 ]; then echo "negative"; elif [ $x -eq 0 ]; then echo "zero"; else echo "positive"; fi
说明
else is a shell keyword providing an alternative branch in conditional statements. It executes when the preceding if (and any elif) conditions are false. else appears after if/elif blocks and before fi. It cannot have a condition - it catches all remaining cases. Only one else block is allowed per if statement. This is a fundamental shell scripting construct for handling the "otherwise" case in conditional logic.
FAQ
What is the else command used for?
else is a shell keyword providing an alternative branch in conditional statements. It executes when the preceding if (and any elif) conditions are false. else appears after if/elif blocks and before fi. It cannot have a condition - it catches all remaining cases. Only one else block is allowed per if statement. This is a fundamental shell scripting construct for handling the "otherwise" case in conditional logic.
How do I run a basic else example?
Run `if [condition]; then echo "true"; else echo "false"; fi` in a terminal, then adjust file names, paths, flags, or remote targets for your system.
Where can I find more else examples?
This page includes 2 examples for else, plus related commands for nearby Linux tasks.