Prepare for your next Shell Scripting interview with this comprehensive guide featuring 30 essential questions. Covering basic, intermediate, and advanced topics, these questions and detailed answers are tailored for freshers, candidates with 1-3 years of experience, and professionals with 3-6 years in the field. Each explanation includes practical examples and code snippets to help you understand and apply concepts effectively.
Basic Shell Scripting Interview Questions (1-10)
1. What is a shell in Shell Scripting?
The shell is a command-line interpreter that provides an interface between the user and the operating system kernel. It reads commands entered by the user, interprets them, and executes the appropriate programs. Common shells include bash, sh, and ksh.
2. What is a Shell Script?
A Shell Script is a text file containing a sequence of commands that the shell can execute. It automates repetitive tasks and can include variables, loops, conditionals, and functions. Scripts typically start with a shebang line like #!/bin/bash to specify the shell interpreter.
3. What is the purpose of the shebang line in a shell script?
The shebang line (e.g., #!/bin/bash) at the top of a script tells the system which interpreter to use to execute the script. Without it, the system uses the default shell specified in the environment.
4. How do you run a shell script in the background?
To run a script in the background, append & to the command. For example:
./myscript.sh &
5. What does the $? variable represent?
The $? variable holds the exit status of the last executed command. A value of 0 indicates success, while any non-zero value indicates failure.
6. What are the different types of variables in shell scripting?
There are two main types: system-defined variables (created by the shell, like $PATH, $HOME) and user-defined variables (created by the user, like myvar="value").
7. What does $# represent in shell scripting?
$# gives the number of positional parameters (arguments) passed to the script.
8. What are the standard streams in shell scripting?
The three standard streams are:
- Standard Input (stdin) – file descriptor 0
- Standard Output (stdout) – file descriptor 1
- Standard Error (stderr) – file descriptor 2
9. How do you make a shell script executable?
Use the chmod command: chmod +x script.sh. This sets the execute permission on the file.
10. What is the difference between echo and printf?
echo automatically adds a newline and is simpler, while printf provides formatted output with more control over spacing and no automatic newline.
Intermediate Shell Scripting Interview Questions (11-20)
11. How do you read user input in a shell script?
Use the read command: read variable_name. Example:
echo "Enter your name: "
read name
echo "Hello, $name"
12. Explain positional parameters with an example.
Positional parameters ($1, $2, etc.) represent arguments passed to the script. $0 is the script name. Example:
#!/bin/bash
echo "First arg: $1"
echo "Second arg: $2"
Running ./script.sh apple banana outputs: First arg: apple, Second arg: banana.
13. How do you compare strings in shell scripting?
Use the test command or [ ]: [ "$str1" = "$str2" ] for equality, [ "$str1" != "$str2" ] for inequality.
14. What is the continue command used for?
The continue statement skips the rest of the current loop iteration and proceeds to the next one. It’s used in for and while loops.
15. In a Zoho DevOps scenario, how would you check if a log file is empty before processing?
Use [ -s file ] to check if the file is non-empty: if [ ! -s /var/log/app.log ]; then echo "Log is empty"; fi.
16. Explain the use of sed with an example.
sed is a stream editor for filtering and transforming text. Example to replace “old” with “new”: sed 's/old/new/g' file.txt.
17. How do you use awk to print specific fields?
awk processes text files by fields. Example to print first field: awk '{print $1}' file.txt.
18. What is the difference between single and double quotes?
Single quotes ('text') treat content literally (no expansion). Double quotes ("text") allow variable expansion and command substitution.
19. How do you create a function in shell scripting?
Define functions using: function_name() { commands; }. Call it by name: function_name.
20. At Salesforce, how might you use grep to find errors in logs?
Use grep "ERROR" /var/log/app.log to search for lines containing “ERROR”. Add -i for case-insensitive, -r for recursive.
Advanced Shell Scripting Interview Questions (21-30)
21. How do you debug a shell script?
Use set -x to enable debug mode (prints commands before execution). Also use set -u for undefined variables and manual debug echoes.
22. What happens when you use the exec command?
exec replaces the current shell process with a new one, without creating a child process. The script terminates after execution.
23. Explain process states in Linux shell context.
Processes go through: Running (executing), Waiting (resource wait), Stopped (suspended), Zombie (terminated but not reaped).
24. For an Atlassian tool deployment, write a script snippet to backup directories.
#!/bin/bash
backup_dir="/backup/$(date +%Y%m%d)"
mkdir -p $backup_dir
tar -czf $backup_dir/app.tar.gz /opt/app
25. How do you handle signals in shell scripts?
Use the trap command: trap "cleanup; exit" SIGINT SIGTERM to handle interrupts and define cleanup actions.
26. What is an array in bash and how do you use it?
Bash arrays store multiple values: arr=(item1 item2 item3). Access with ${arr[0]} or all with ${arr[@]}.
27. In an Adobe automation scenario, how do you loop through files matching a pattern?
for file in *.log; do
echo "Processing $file"
gzip $file
done
28. Explain tee command usage.
tee reads from stdin and writes to both stdout and files: command | tee output.log.
29. How do you implement error handling in scripts?
Use set -e to exit on any error, combined with if checks on command exit status: command || { echo "Failed"; exit 1; }.
30. For a Swiggy batch processing script, explain trapping exit with cleanup.
#!/bin/bash
trap 'echo "Cleaning up..."; rm -f /tmp/tempfile' EXIT
# Script logic here
echo "Processing complete"
This ensures cleanup runs on normal exit, error, or interrupt.