70 Common Shell Script Interview Questions and Answers
This article compiles 70 typical Linux shell scripting interview questions covering script arguments, parameter handling, condition checks, loops, string manipulation, array operations, file I/O, and common command usages, each accompanied by concise Bash code examples to help candidates prepare effectively.
Shell scripting knowledge is essential for system administrators and is frequently tested in Linux job interviews; this guide presents 70 interview‑style questions with clear Bash solutions.
Passing arguments to a script – run a script with ./script argument and access it via $1, $2, etc.
Using parameters inside a script – example copies a file: ./copy.sh file1.txt /tmp/ with script body #!/bin/bash and cp $1 $2.
Counting arguments – $# returns the number of supplied arguments.
Script name and exit status – $0 gives the script name, $? provides the previous command’s exit code, and $? can be used in conditional checks.
File line extraction – get the 10th line with head -10 file | tail -1, the first line with head -1 file, and the last line with tail -1 file.
Loop constructs – Bash supports for, while, and until loops, e.g., for i in $(ls); do echo $i; done or a counting loop while [ $COUNTER -lt 10 ]; do ...; let COUNTER+=1; done.
Conditional tests – numeric comparison uses -gt, -lt, -eq; string comparison uses == inside [[ ... ]]. Example: [[ $string == abc* ]] checks a prefix.
Variable manipulation – length: ${#variable}; substring: ${variable:11:6}; replace: ${variable//pattern/replacement}; remove spaces: echo $string | tr -d " ".
Array handling – define an array: array=("Hi" "my" "name" "is"); access first element ${array[0]}; list all elements ${array[@]}; get indices ${!array[@]}; delete an element unset array[2]; add a new element array[333]="New_element".
Process control – run a command in background with command & or use nohup command & for resilience; redirect output with exec >log.txt 2>&1.
Miscellaneous utilities – count users with wc -l /etc/passwd, list users whose UID is below 100 using awk -F: '$3<100' /etc/passwd, or filter usernames starting with specific prefixes via egrep "^ab|^xy" /etc/passwd | cut -d: -f1.
Overall, the collection equips interviewees with practical Bash snippets for common scripting tasks, from basic syntax to more advanced pattern matching and process management.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architects Research Society
A daily treasure trove for architects, expanding your view and depth. We share enterprise, business, application, data, technology, and security architecture, discuss frameworks, planning, governance, standards, and implementation, and explore emerging styles such as microservices, event‑driven, micro‑frontend, big data, data warehousing, IoT, and AI architecture.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
