Conditionals and Loops
As with any other general-purpose programming language, the shell provides conditional and looping constructs. They are especially useful within shell scripts. We will learn some of the commonly used conditionals and loops in further sections.
Note
Conditionals and loops (along with functions, which we will cover later) make the shell language Turing complete. This is a computer science notion that means that it is equivalent to any general programming language in terms of its capabilities.
Conditional Expressions
As we saw in the previous chapter, every command returns an exit code that is interpreted as true and false for zero and non-zero values, respectively. A conditional expression is any shell command evaluating to true or false. One mechanism that augments this is the [[ ]]
construct, which provides a way to test for a condition and return a Boolean. This syntax is as follows:
[[ EXPRESSION ]]
Here, EXPRESSION
has a syntax similar to...