7. Decisions, Decisions

This chapter introduces the conditional statement, a construct that is present in almost every programming language: if. It enables you to test a condition and then change the flow of program execution based on the result of the test.

The general format of the if command is

if commandt then         command         command         ... fi

where commandt is executed and its exit status is tested. If the exit status is zero, the commands that follow between then and fi are executed; otherwise, they’re skipped.

Exit Status

To understand how conditional tests work, it’s important to know about how Unix works with what’s called the exit status. Whenever any program completes execution, it returns an exit status code to the shell. ...

Get Shell Programming in Unix, Linux and OS X, Fourth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.