Posted in

Top 30 Shell Scripting Interview Questions and Answers for All Experience Levels

Prepare for your next Shell Scripting interview with this comprehensive guide featuring 30 essential questions. Covering basic concepts to advanced scenarios, these questions help freshers, candidates with 1-3 years experience, and professionals with 3-6 years prepare effectively. All examples use pure Shell Scripting techniques.

Basic Shell Scripting Interview Questions (1-10)

1. What is a shell script?

A shell script is a text file containing a sequence of shell commands executed by the shell interpreter to automate repetitive tasks.[1][3]

2. What is the purpose of the shebang (#!) line in a shell script?

The shebang line specifies the shell interpreter to execute the script, such as #!/bin/bash for Bash or #!/bin/sh for POSIX shell.[4]

3. How do you run a shell script in the background?

Add & at the end of the command: ./script.sh &[1]

4. What does the $? variable represent?

$? returns the exit status of the last executed command (0 for success, non-zero for failure).[1]

5. How do you add comments in a shell script?

Use the # symbol. Everything after # on a line is ignored by the shell.

#!/bin/bash
# This is a comment
echo "Hello World"

[3]

6. What is the difference between $ and $$?

$ references a variable value, while $$ represents the Process ID (PID) of the current shell.[3]

7. How do you make a shell script executable?

Use chmod +x script.sh to add execute permission.[2]

8. What are positional parameters in shell scripting?

Positional parameters ($1, $2, etc.) represent command-line arguments passed to the script.[4]

9. What does $0 represent?

$0 contains the name of the script itself.[4]

10. How do you print the current working directory in a script?

Use the pwd command: echo $(pwd)[2]

Intermediate Shell Scripting Interview Questions (11-20)

11. What are the different types of shell variables?

Local variables (script-specific), environment variables (exported with export), and special variables like $@, $#.[5]

12. How do you check if a file exists in a shell script?

Use [ -f filename ] for regular files or [ -e filename ] for any file type.

if [ -f /etc/passwd ]; then
  echo "File exists"
fi

[2]

13. Explain the use of > and >> operators.

> redirects output and overwrites the file, while >> appends to the file.[1]

14. How do you read user input in a shell script?

Use the read command: read username[3]

15. Write a script to check if a number is positive, negative, or zero.

#!/bin/bash
echo "Enter a number:"
read num
if [ $num -gt 0 ]; then
  echo "Positive"
elif [ $num -lt 0 ]; then
  echo "Negative"
else
  echo "Zero"
fi

[3]

16. What is the difference between [ ] and [[ ]]?

[ ] is POSIX test command; [[ ]] is Bash keyword with better string handling and no word splitting.[2]

17. How do you loop through all files in a directory?

Use for loop:

for file in *; do
  echo "$file"
done

[1]

18. What does $# represent?

$# gives the number of positional parameters (arguments) passed to the script.[4]

19. How do you substitute the value of a variable in a string?

Use double quotes: name="John"; echo "Hello $name"[3]

20. At Zoho, how would you create a script to backup log files daily?

#!/bin/bash
tar -czf /backup/logs_$(date +%Y%m%d).tar.gz /var/log/*.log

[1]

Advanced Shell Scripting Interview Questions (21-30)

21. Explain the three file permissions in shell scripting.

Read (r), Write (w), and Execute (x) permissions control access for owner, group, and others.[2]

22. How do you handle signals in shell scripts?

Use trap command: trap 'echo "Script interrupted"' INT[5]

23. Write a script to find and print numbers divisible by both 3 and 5 up to 100.

#!/bin/bash
for i in {1..100}; do
  if [ $((i%3)) -eq 0 ] && [ $((i%5)) -eq 0 ]; then
    echo $i
  fi
done

[6]

24. What is the purpose of set -e?

set -e exits the script immediately if any command returns a non-zero status.[5]

25. How do you parse command-line arguments in a script?

Use while case with shift:

while [ $# -gt 0 ]; do
  case $1 in
    -f) shift; file=$1 ;;
  esac
  shift
done

[4]

26. For Atlassian tools monitoring, write a script to check disk usage and alert if over 80%.

#!/bin/bash
usage=$(df / | grep / | awk '{print $5}' | sed 's/%//g')
if [ $usage -gt 80 ]; then
  echo "Disk usage critical: $usage%"
fi

[3]

27. Explain awk field separators with an example.

$1, $2 represent first and second fields: awk '{print $1}' file.txt prints first column.[1]

28. How do you create a function in shell scripting?

myfunc() {
  echo "Function called"
}
myfunc

[5]

29. At Salesforce, how would you write a script to rotate logs older than 7 days?

#!/bin/bash
find /var/log -name "*.log" -mtime +7 -exec rm {} \;

[1]

30. What is array handling in Bash, and provide an example?

Bash supports arrays:

fruits=("apple" "banana" "orange")
echo ${fruits[1]}  # banana
echo ${fruits[@]}  # all elements

[4]

Leave a Reply

Your email address will not be published. Required fields are marked *