Shell Scripting and Automation Questions
Automating tasks with the command line: Bash and shell scripting, pipes and redirection, text processing, and writing custom tooling and monitoring scripts to glue systems together. Covers the scripting fundamentals infrastructure and platform engineers use daily, including automation-oriented use of general-purpose scripting. Distinct from application programming in its throwaway-to-durable tooling mindset.
Describe best practices for safely editing files in-place (for example config files) using sed or awk inside scripts. Provide a robust pattern that preserves file permissions and ownership, avoids data loss if the script is interrupted, and handles systems where 'sed -i' syntax differs between GNU and BSD variants.
Explain traps in shell scripting. Provide example usages: a trap on EXIT to clean up temporary resources even when the script exits early under 'set -e', a trap on SIGINT to perform graceful shutdown, and an ERR trap to log the command that failed. Discuss limitations of traps across shells and in subshells.
Discuss portability challenges when writing shell scripts that must run across Linux distributions and BSD/macOS. Give concrete examples of differences in 'stat', 'sed -i', 'readlink', and 'date' behavior/flags and propose detection and fallback strategies. Provide a short snippet that detects GNU utilities and uses fallbacks when necessary.
Write a performant, memory-efficient Bash pipeline or script that scans a 10GB newline-delimited JSON log file, extracts the top-level string field 'user_id' from each JSON object, counts the number of unique user_ids, and tolerates malformed JSON lines by skipping them while printing a warning to stderr. Use only standard Unix utilities commonly available on Linux and explain optimizations you made for performance and low memory usage.
Describe what the shell options 'set -euo pipefail' do in Bash scripts. For each option (-e, -u, and -o pipefail) explain its behavior, typical use in defensive scripting, and common caveats including interactions with pipelines, subshells, command lists, and conditional tests. Suggest safer patterns to use alongside these options.
Unlock Full Question Bank
Get access to all 30 Shell Scripting and Automation interview questions and detailed answers.
Sign in to ContinueJoin thousands of developers preparing for their dream job.