Two Bash Parameter Expansion Quirks That Commonly Cause Script Failures
A sysadmin describes two bash behaviors that often pass code review yet lead to intermittent production failures. The first concerns ${var:-default} versus ${var-default}: the colon form substitutes the default for both unset and empty variables, while the form without the colon only does so when the variable is unset. The second contrasts [[ -v var ]], which tests whether a variable is declared, with [[ -n $var ]], which tests for a non-empty value, so a declared but empty variable is detected only by the former. The author notes that -v correctly identifies flags passed as empty strings where -n misses them. The piece recommends the colon-default form and the -v test for defensive scripting that behaves consistently across bash versions.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in