Best practice#

You might have seen bareword variables like x in Bash arithmetic, but the use of "$x" above is deliberate. First, "$x" is consistent with how variables are used in general, and using different syntax in arithmetic contexts is just a needless complexity. Second, a subtle difference is that if $x is not set x will be coerced to zero while "$x" results in a syntax error. Debugging a rogue zero is going to be much more difficult than a syntax error, so we should always quote within arithmetic contexts. Example:

 

This page is a preview of The newline Guide to Bash Scripting

No discussions yet