Interview Prep/Shell Scripting/Troubleshooting: quoting, expansion and tool-input bugs

A disk check fails with `[: 93%: integer expression expected`

BeginnerTroubleshootingTroubleshooting: quoting, expansion and tool-input bugs
Answer

Likely causes → The value contains non-digits: the % from df, a trailing \r, whitespace, a decimal (93.5), or an empty string.

Diagnose → printf '%q\n' "$pct" reveals hidden characters like $'93%\r'.

Fix → Clean the value before comparing:

pct=$(df -P / | awk 'NR == 2 {gsub(/%/, "", $5); print $5}')
[[ $pct =~ ^[0-9]+$ ]] || die "unexpected df output: $pct"
(( pct >= 90 )) && alert

Prevent → Validate numeric input with a regex before arithmetic; for decimals compare with awk.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Troubleshooting: quoting, expansion and tool-input bugs