Shell scripting is an essential skill for system administrators, DevOps engineers, and software developers. Below is a list of top 50 frequently asked interview questions along with their answers.
Table of Contents
Toggleπ Basic Shell Scripting Questions
1. What is Shell Scripting?
A shell script is a program written using shell commands and scripting constructs to automate tasks in UNIX/Linux.
2. What are different types of shells available in Linux?
-
Bash (Bourne Again Shell) β Default shell in most Linux distros
-
Sh (Bourne Shell) β Original UNIX shell
-
Ksh (Korn Shell) β Extended features of the Bourne Shell
-
Csh (C Shell) β C-like syntax shell
-
Zsh (Z Shell) β Advanced shell with themes and plugins
3. How do you execute a shell script?
bash script.sh
or
chmod +x script.sh
./script.sh
4. How do you define a variable in a shell script?
name="John"
echo "Hello, $name"
5. How do you read user input in a script?
echo "Enter your name: "
read name
echo "Hello, $name!"
6. How do you check if a file exists in a shell script?
if [ -f "file.txt" ]; then
echo "File exists"
else
echo "File does not exist"
fi
7. How do you check if a directory exists?
if [ -d "/path/to/dir" ]; then
echo "Directory exists"
else
echo "Directory does not exist"
fi
π Intermediate Shell Scripting Questions
8. How do you write an if-else
statement in shell scripting?
if [ "$num" -gt 10 ]; then
echo "Number is greater than 10"
else
echo "Number is 10 or less"
fi
9. How do you use a case
statement in shell scripting?
echo "Enter a fruit: "
read fruit
case $fruit in
apple) echo "You chose apple";;
banana) echo "You chose banana";;
orange) echo "You chose orange";;
*) echo "Unknown fruit";;
esac
10. How do you perform arithmetic operations in shell scripting?
num1=10
num2=5
sum=$((num1 + num2))
echo "Sum: $sum"
11. What is the difference between =
and ==
in shell scripting?
-
=
is used for string assignment. -
==
is used for string comparison inside[ ]
brackets.
12. How do you create and use an array in a shell script?
fruits=("Apple" "Banana" "Cherry")
echo "First fruit: ${fruits[0]}"
echo "All fruits: ${fruits[@]}"
13. How do you loop through an array in shell scripting?
for fruit in "${fruits[@]}"; do
echo "$fruit"
done
14. How do you check if a variable is empty?
if [ -z "$var" ]; then
echo "Variable is empty"
fi
15. How do you define a function in shell scripting?
my_function() {
echo "Hello from function"
}
my_function
16. How do you pass arguments to a shell script?
./script.sh arg1 arg2
echo "First argument: $1"
echo "Second argument: $2"
π Advanced Shell Scripting Questions
17. How do you get the last executed commandβs exit status?
echo $?
(0 = success, non-zero = failure)
18. How do you redirect output to a file in shell scripting?
echo "Hello" > output.txt
19. How do you append output to a file instead of overwriting?
echo "New line" >> output.txt
20. How do you check if a command executed successfully?
if ls /invalid/path &> /dev/null; then
echo "Success"
else
echo "Command failed"
fi
21. What is the difference between >
and >>
?
-
>
overwrites a file -
>>
appends to a file
22. How do you schedule a cron job in Linux?
crontab -e
Example: Run script every day at 2 AM
0 2 * * * /path/to/script.sh
23. How do you find the number of lines in a file?
wc -l file.txt
24. How do you replace text in a file using sed
?
sed -i 's/old-text/new-text/g' file.txt
25. How do you filter output using grep
?
grep "error" logfile.txt
π Expert-Level Shell Scripting Questions
26. What is the difference between $*
and $@
?
-
$*
treats all arguments as a single string -
$@
treats arguments as separate words
27. How do you execute multiple commands in one line?
command1 && command2
28. What is the difference between &&
and ||
?
-
&&
runs the next command only if the first one succeeds -
||
runs the next command only if the first one fails
29. How do you count occurrences of a word in a file?
grep -o "word" file.txt | wc -l
30. What is a here document in shell scripting?
A here document allows multi-line input:
cat << EOF
This is a multi-line text
EOF
31. How do you run a command in the background?
long_running_command &
32. How do you create a log file for a script?
./script.sh > script.log 2>&1
33. How do you kill a process using a script?
kill -9 PID
34. How do you list open network connections?
netstat -tulnp
35. How do you find the disk usage of a directory?
du -sh /path/to/dir
π Advanced & Expert-Level Shell Scripting Questions (36-50)
36. How do you find all running processes for a specific user?
ps -u username
Example:
ps -u root
Shows all processes running under the root user.
37. How do you check the CPU and memory usage in Linux?
top
or
htop # (if installed)
For a snapshot of CPU & memory usage:
free -m
38. How do you find and delete files older than 7 days?
find /path/to/directory -type f -mtime +7 -exec rm {} \;
-
-mtime +7
β Finds files older than 7 days -
-exec rm {}
β Deletes the found files
39. How do you write a script that automatically restarts a service if it crashes?
#!/bin/bash
SERVICE="nginx"
if ! pgrep -x "$SERVICE" > /dev/null; then
echo "$SERVICE is not running. Restarting..."
systemctl start $SERVICE
else
echo "$SERVICE is running."
fi
This script checks if nginx is running and restarts it if necessary.
40. How do you extract a column from a text file?
Using awk
:
awk '{print $2}' file.txt
This prints the second column from each line of file.txt
.
41. How do you replace spaces with underscores in all filenames in a directory?
for file in *\ *; do
mv "$file" "${file// /_}"
done
This renames all files by replacing spaces with underscores.
42. How do you find and replace text in multiple files using sed
?
sed -i 's/old-text/new-text/g' *.txt
This replaces "old-text"
with "new-text"
in all .txt
files.
43. How do you create an infinite loop in a shell script?
while true; do
echo "Running..."
sleep 5
done
This runs indefinitely, printing βRunningβ¦β every 5 seconds.
44. How do you delete empty files inside a directory?
find /path/to/dir -type f -empty -delete
Deletes all empty files in the specified directory.
45. How do you rename all .txt
files to .bak
in a folder?
for file in *.txt; do
mv "$file" "${file%.txt}.bak"
done
This renames file.txt to file.bak for all .txt
files.
46. How do you run a command as another user inside a script?
Using su
(Switch User):
su - username -c "command_to_run"
Example:
su - john -c "ls -l /home/john"
This runs ls -l /home/john
as user John.
47. How do you store the output of a command into a variable?
output=$(ls -l)
echo "$output"
This stores the output of ls -l
inside the $output
variable.
48. How do you schedule a script to run every hour using crontab?
1οΈβ£ Open crontab:
crontab -e
2οΈβ£ Add the following line:
0 * * * * /path/to/script.sh
This runs the script every hour at minute 0.
49. How do you extract the filename from a full file path?
file_path="/home/user/file.txt"
filename=$(basename "$file_path")
echo "$filename"
Output:
file.txt
50. How do you create a shell script that runs only once per day?
Use a lock file to prevent multiple executions:
#!/bin/bash
LOCK_FILE="/tmp/myscript.lock"
if [ -e "$LOCK_FILE" ]; then
echo "Script already ran today."
exit 1
fi
# Run the main script tasks here
echo "Running the script..."
# Create a lock file
touch "$LOCK_FILE"
Then, schedule it in crontab to run daily:
0 0 * * * /path/to/script.sh