Day 4:Shell Scripting Interview Questions

Aditya TiwaryAditya Tiwary
5 min read

:Basic Shell Scripting Questions

1. What is a shell? What is shell scripting?
A shell is a command-line interpreter that provides a user interface for the Unix/Linux operating system. Shell scripting is writing a series of command-line instructions in a file to automate tasks.

2. What are different types of shells available in Linux?

  • Bourne Shell (sh)

  • Bourne Again Shell (bash)

  • Korn Shell (ksh)

  • C Shell (csh)

  • Z Shell (zsh)

3. How do you create a shell script?
Create a file with .sh extension and add the shell commands. Example:

#!/bin/bash
echo "Hello, World!"

Save as script.sh, then make it executable.

4. What is the difference between sh, bash, and #!/bin/bash?

  • sh is the Bourne shell.

  • bash is an enhanced version of sh.

  • #!/bin/bash is a shebang that tells the OS to use bash to execute the script.

5. How do you make a script executable?

chmod +x script.sh

6. What is the difference between . and source?
Both are used to execute a script in the current shell without creating a subshell. source is a bash built-in; . is POSIX-compliant.

7. Explain the use of shebang (#!/bin/bash).
Shebang defines the interpreter that should be used to execute the script.

8. How do you read input from the user in a script?

read -p "Enter your name: " name
echo "Hello, $name"

9. How do you pass arguments to a shell script?
Arguments can be passed as: ./script.sh arg1 arg2. Access using $1, $2, etc.

10. How can you access script arguments like $0, $1, $@, $*, $#?

  • $0: Script name

  • $1, $2, ...: Positional arguments

  • $@: All arguments as separate words

  • $*: All arguments as a single word

  • $#: Number of arguments


Intermediate Shell Scripting (Logic + DevOps Context)

11. How do you debug a shell script?
Use bash -x script.sh or add set -x at the top of the script.

12. What’s the difference between == and -eq in conditionals?

  • ==: Used for string comparison

  • -eq: Used for numeric comparison

13. How do you handle errors in a shell script?
Use set -e to exit on error, trap to catch signals.

14. Write a script to monitor disk usage and alert if usage > 80%.

#!/bin/bash
THRESHOLD=80
USAGE=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')
if [ $USAGE -gt $THRESHOLD ]; then
  echo "Disk usage is above $THRESHOLD%. Currently at $USAGE%." | mail -s "Disk Alert" admin@example.com
fi

15. Write a script to check if a service is running and restart if down.

#!/bin/bash
SERVICE=nginx
if ! pgrep -x "$SERVICE" > /dev/null
then
  echo "$SERVICE is not running. Restarting..."
  systemctl start $SERVICE
fi

16. How do you loop through files or command outputs?

for file in *.log; do
  echo "Processing $file"
done

17. What is the difference between > and >>?

  • >: Overwrites file

  • >>: Appends to file

18. How do you schedule a script to run periodically?
Use cron:

crontab -e
*/5 * * * * /path/to/script.sh

19. How do you use grep, awk, sed inside a script?

  • grep: search

  • awk: pattern scanning and processing

  • sed: stream editing

Example:

grep "ERROR" /var/log/syslog | awk '{print $5}' | sed 's/[][]//g'

20. How do you automate SSH login in a script without password?
Set up SSH key-based auth using ssh-keygen and ssh-copy-id.


Advanced Shell Scripting for DevOps

21. Script to deploy code from Git:

#!/bin/bash
git pull origin main
npm install
pm run build
systemctl restart myapp

22. Automate infrastructure setup: Use shell to install packages, create directories, and configure services.

apt update && apt install nginx -y
mkdir -p /var/www/html

23. Use of expect:

#!/usr/bin/expect
spawn ssh user@host
expect "password:"
send "yourpassword\r"
interact

24. Shell scripts in CI/CD:
Used to run tests, deploy code, configure environments inside Jenkins/GitLab pipelines.

25. Backup logs/config files:

tar czf /backup/logs_$(date +%F).tar.gz /var/log/*.log

26. Idempotent scripts:
Scripts that can be run multiple times without changing the result, e.g., checking if a user exists before adding.

27. Trap and signal handling:

trap "echo 'Interrupted'; exit" SIGINT SIGTERM

28. POSIX-compliant script:
Avoid bash-only features; use /bin/sh as shebang.

29. Parse JSON/YAML:

jq '.key' file.json
yq eval '.key' file.yaml

30. Best practices:

  • Use set -euo pipefail

  • Log everything

  • Handle errors

  • Validate inputs


DevOps Tool-Specific Use Cases

31. Shell in Docker entrypoint:
Used to start multiple services or init configs.

#!/bin/bash
service nginx start
exec "$@"

32. Bootstrap a K8s node:
Install kubeadm, kubelet, and join the cluster via shell.

33. Shell in Terraform/Ansible:
Use local-exec in Terraform, or shell module in Ansible.

34. Script in Helm/K8s Job:
Used as initContainers or job containers with entrypoint as shell script.

35. Troubleshoot production issue:
A shell script to collect logs, check CPU/memory, analyze failed services.


Scenario-Based

36. Run command on 100 servers:

for server in $(cat servers.txt); do
  ssh user@$server "uptime"
done

37. Ping servers and log down hosts:

for host in $(cat hosts.txt); do
  ping -c1 $host &> /dev/null || echo "$host is down" >> down.log
done

38. Fetch logs from multiple servers:

for server in $(cat servers.txt); do
  scp user@$server:/var/log/syslog ./logs/$server.log
done

39. Limitations of shell scripting:

  • Poor debugging support

  • Complex logic is hard to maintain

  • Not cross-platform

40. When to prefer Python over shell:

  • Complex logic and better libraries

  • Better error handling

  • Multi-threading and object-oriented programming

0
Subscribe to my newsletter

Read articles from Aditya Tiwary directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Aditya Tiwary
Aditya Tiwary