Understanding Subshells
When you perform a test using the [ $var -ne 0 ]
construct, the test will invoke a subshell. To prevent a test from invoking a subshell, use this construct, instead:
[[ $var -ne 0 ]]
This can make your script run somewhat more efficiently, which might or might not be a huge deal for your particular script.
This [[. . .]]
type of construct is also necessary when you perform tests that require matching a pattern to a regular expression. (Matching regular expressions won’t work within a [. . .]
construct.)
The downside of this [[. . .]]
construct is that you can’t use it on certain non-bash
shells, such as dash
, ash
, or Bourne
. (You’ll see that in Chapter 19, Shell Script Portability.)
Of course, you don’t know what regular expressions are just yet, and that’s okay. I’ll show you all about them in Chapter 9, Filtering Text with grep, sed, and Regular Expressions.
At any rate, you...