Conditionals: if/then/else
A Bash if
statement is usually found in this form:
if [[ $TEST ]]; then $STATEMENTS else $OTHER_STATEMENTS fi
Remember a few things about this form before we look at examples:
if
andfi
begin and terminate theif
block, respectively.;
delimits statements in Bash; add one right after the test.[[
and]]
delimit your test expression.- The
else
clause is optional.
Here’s what the if
statement looks like in Bash:
if [[ -e "example.txt" ]]; then
echo "The file exists!"
ifelse
If you want to tack an else
clause onto this structure, you can!
if [[ -e "example.txt" ]]; then
echo "The file exists!"
else
echo "The file does not exist!"
fi