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.

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

Updated

In other subjects