if statement
A statement that runs a block of code only when a condition holds, optionally with an else block for when it does not.
In more detail
Most languages add further cases with else if or elif. In Python the condition is an expression judged true or false and the block is indented. In shell scripts the condition is a command judged by its exit status, and fi closes the block. The shape is the same whether the block has one statement or many.
Examples
if grep -q bash poem.txt; then echo "found it"; fiif weight > 50:
print("Extra charge")
else:
print("OK")Related terms
- test command — The test command, also written with single square brackets, that checks a condition such as a file existing or two values being equal, and exits 0 if it holds.
- double brackets — Bash's double-bracket test, [[ and ]], a keyword that does not split words or expand globs and adds pattern and regular expression matching.
- exit code — A number a process returns when it finishes: 0 means success, anything else means something went wrong.
- case statement — A statement that compares one value against several glob patterns and runs the block for the first that matches.
- condition — An expression that is tested for being true or false, such as weight > 50, to decide what a program does next.
- code block — The group of indented statements under a heading such as def, if, for or while, which run together as one unit.
- conditional expression — An expression that checks a condition and evaluates to one of two values, one for true and one for false.
Taught in
- Which Shell Am I In, And Getting Bash Five — Bash Scripting & Command Line Automation
- Exit Codes, And What Success Means — Bash Scripting & Command Line Automation
- Conditionals: test, Brackets, Double Brackets, case — Bash Scripting & Command Line Automation
- set -e, set -u And pipefail: Promises And Traps — Bash Scripting & Command Line Automation
- Debugging: set -x, bash -n And Reading The Error — Bash Scripting & Command Line Automation
- Conditions And Simple If Statements — Hands-on Python: Complete Video Course & Book
- If Else, And Conditional Expressions — Hands-on Python: Complete Video Course & Book
- If Elif Chains — Hands-on Python: Complete Video Course & Book
- Nesting Control Flow Statements — Hands-on Python: Complete Video Course & Book
- Thirty Questions, One Sneaky Print — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Fundamentals Sprint — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Branching With If, Elif And Else — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Conditions The Exam Loves — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- While, For, Range And The Loop Else — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Nested Loops, Traced By Hand — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Try, Except, Else And Finally — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Twelve Traps, One Shape Each — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- Mock, Review And Exam Day — PCEP Certified Entry-Level Python Programmer (PCEP-30-02) Exam Prep
- List Comprehensions, Filtered And Nested — PCAP Certified Associate in Python Programming (PCAP-31-03) Exam Prep