# if Statement

*noun*

say it: **“IF STAYT-ment”** **/ˈɪf ˈsteɪtmənt/**

A part of a script that runs some commands only when a test passes, and other commands when it fails.

## What is an **if statement** in bash?

An **if statement** lets a script choose. Bash runs the command after `if`, often a test in `[[ ]]`; when it works (exit code 0), the lines after `then` run, otherwise `elif` tries another test and `else` catches the rest. `fi`, `if` backwards, ends it.

```bash
if [[ $answer == yes ]]; then
  cp essay.txt backup/
else
  echo "Nothing was backed up."
fi
```

## In a sentence

- “The **if statement** copies the essay only when the answer is yes.”
- “An **if statement** with elif lets the backup script tell a missing file from an empty one.”

Level: Apprentice · [More Unix terms at this level](https://www.ask-codey.com/glossary?category=unix&level=2)

Updated: November 10, 2026

## In other subjects

- [if Statement in C#](https://www.ask-codey.com/glossary/if-statement-c-sharp)
- [If Statement in C#](https://www.ask-codey.com/glossary/if-statement-c-style)
- [if Statement in Java](https://www.ask-codey.com/glossary/if-statement-java)
- [if Statement in JavaScript](https://www.ask-codey.com/glossary/if-statement-javascript)
- [if Statement in PowerShell](https://www.ask-codey.com/glossary/if-statement-powershell)
- [if Statement in Python](https://www.ask-codey.com/glossary/if-statement)

Source: https://www.ask-codey.com/glossary/if-statement-unix
