Rapid fire: what does each of these print or do?
IntermediateConceptBest practices and rapid-fire commands
Answer
echo "${#PATH}" # length of PATH in characters
echo "${0##*/}" # script name without directory
echo "$(( 7 / 2 ))" # 3 (integer division)
set -- a "b c"; echo $# # 2
x=; echo "${x:-empty}" # empty
true | false; echo $? # 1 (last command)
false | true; echo $? # 0 (1 with pipefail)
[[ 10 < 9 ]] && echo yes # yes (string comparison)
printf '%q\n' "it's a test" # it\'s\ a\ test
type cd # cd is a shell builtin
Interviewers use questions like these to check you understand expansion and exit-status rules, not just syntax.
More on Best practices and rapid-fire commands
What does a production-ready script template look like?AdvancedWhat logging practices do you follow in automation scripts?IntermediateHow do you make a script support a dry-run mode cleanly?BeginnerRapid fire: one-liners for disk, memory and process checks.BeginnerRapid fire: one-liners for logs and text.BeginnerRapid fire: one-liners for files and housekeeping.Beginner