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

`awk "{print $2}" file` prints whole lines instead of the second column.

IntermediateTroubleshootingTroubleshooting: quoting, expansion and tool-input bugs
Answer

Likely causes → Double quotes let the shell expand $2 first (to empty, or the script's second argument), so awk receives {print }, which prints the whole line.

Diagnose → set -x shows awk '{print }' file.

Fix → Single-quote awk programs and pass shell values with -v:

awk -v col="$col" '{print $col}' file

Prevent → Always single-quote awk, sed and jq programs. ShellCheck warns about this pattern.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

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