← 返回命令列表

Linux command

elif 命令

文本

涉及管道、覆盖或删除,执行前请先确认路径和参数。

常用示例

Use elif in

if [condition1]; then echo "first"; elif [condition2]; then echo "second"; fi

Multiple elif

if [ $x -lt 0 ]; then echo "negative"; elif [ $x -eq 0 ]; then echo "zero"; elif [ $x -gt 0 ]; then echo "positive"; fi

说明

elif is a shell keyword used in bash and other POSIX shells for conditional branching. It's short for "else if" and allows multiple conditions to be tested sequentially. elif follows an if statement and precedes then. Multiple elif blocks can appear before an optional else. Only the first matching condition's block executes. This construct is fundamental to shell scripting for implementing multi-way branching logic.

FAQ

What is the elif command used for?

elif is a shell keyword used in bash and other POSIX shells for conditional branching. It's short for "else if" and allows multiple conditions to be tested sequentially. elif follows an if statement and precedes then. Multiple elif blocks can appear before an optional else. Only the first matching condition's block executes. This construct is fundamental to shell scripting for implementing multi-way branching logic.

How do I run a basic elif example?

Run `if [condition1]; then echo "first"; elif [condition2]; then echo "second"; fi` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

Where can I find more elif examples?

This page includes 2 examples for elif, plus related commands for nearby Linux tasks.