Syntax

Bash If / Elif / Else Syntax

In Bash, if / elif / else: chains several conditions — if [[ $x -gt 0 ]]; then ... elif [[ $x -eq 0 ]]; then ... else ... fi

Quick Answer

Syntax
if [[ $x -gt 0 ]]; then ... elif [[ $x -eq 0 ]]; then ... else ... fi
BashSyntax

If / Elif / Else

Code example
if [[ $x -gt 0 ]]; then ... elif [[ $x -eq 0 ]]; then ... else ... fi

Chains several conditions.

Learn More

How to if / elif / else in Bash

  1. Open your Bash file or editor.
  2. Copy the example, then replace its sample names and values for your code.

Bash If / Elif / Else Syntax

Use if [[ $x -gt 0 ]]; then ... elif [[ $x -eq 0 ]]; then ... else ... fi.

Related Syntax References

Frequently asked questions

What is the Bash syntax for if / elif / else?

Use this pattern: if [[ $x -gt 0 ]]; then ... elif [[ $x -eq 0 ]]; then ... else ... fi

Can I copy this Bash code example?

Yes. Use Copy code and adapt the example to your program.

Is this Bash syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify