• Skip to main content
  • Skip to primary sidebar
  • Home
  • WordPress
  • web Hosting
  • linux
  • mysql
  • nginx
  • apache2
  • devops

Raju Ginni

wordpress tutorials seo hosting etc

You are here: Home / Linux sysadmin tutorials linux system administrator / Top 50 Shell Scripting Interview Questions and Answers

Top 50 Shell Scripting Interview Questions and Answers

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?
  • 2. What are different types of shells available in Linux?
  • 3. How do you execute a shell script?
  • 4. How do you define a variable in a shell script?
  • 5. How do you read user input in a script?
  • 6. How do you check if a file exists in a shell script?
  • 7. How do you check if a directory exists?
  • πŸ“Œ Intermediate Shell Scripting Questions
  • 8. How do you write an if-else statement in shell scripting?
  • 9. How do you use a case statement in shell scripting?
  • 10. How do you perform arithmetic operations in shell scripting?
  • 11. What is the difference between = and == in shell scripting?
  • 12. How do you create and use an array in a shell script?
  • 13. How do you loop through an array in shell scripting?
  • 14. How do you check if a variable is empty?
  • 15. How do you define a function in shell scripting?
  • 16. How do you pass arguments to a shell script?
  • πŸ“Œ Advanced Shell Scripting Questions
  • 17. How do you get the last executed command’s exit status?
  • 18. How do you redirect output to a file in shell scripting?
  • 19. How do you append output to a file instead of overwriting?
  • 20. How do you check if a command executed successfully?
  • 21. What is the difference between > and >>?
  • 22. How do you schedule a cron job in Linux?
  • 23. How do you find the number of lines in a file?
  • 24. How do you replace text in a file using sed?
  • 25. How do you filter output using grep?
  • πŸ“Œ Expert-Level Shell Scripting Questions
  • 26. What is the difference between $* and $@?
  • 27. How do you execute multiple commands in one line?
  • 28. What is the difference between && and ||?
  • 29. How do you count occurrences of a word in a file?
  • 30. What is a here document in shell scripting?
  • 31. How do you run a command in the background?
  • 32. How do you create a log file for a script?
  • 33. How do you kill a process using a script?
  • 34. How do you list open network connections?
  • 35. How do you find the disk usage of a directory?
  • πŸ“Œ Advanced & Expert-Level Shell Scripting Questions (36-50)
  • 36. How do you find all running processes for a specific user?
  • 37. How do you check the CPU and memory usage in Linux?
  • 38. How do you find and delete files older than 7 days?
  • 39. How do you write a script that automatically restarts a service if it crashes?
  • 40. How do you extract a column from a text file?
  • 41. How do you replace spaces with underscores in all filenames in a directory?
  • 42. How do you find and replace text in multiple files using sed?
  • 43. How do you create an infinite loop in a shell script?
  • 44. How do you delete empty files inside a directory?
  • 45. How do you rename all .txt files to .bak in a folder?
  • 46. How do you run a command as another user inside a script?
  • 47. How do you store the output of a command into a variable?
  • 48. How do you schedule a script to run every hour using crontab?
  • 49. How do you extract the filename from a full file path?
  • 50. How do you create a shell script that runs only once per day?

πŸ“Œ 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
bash script.sh

or

bash
chmod +x script.sh
./script.sh

4. How do you define a variable in a shell script?

bash
name="John"
echo "Hello, $name"

5. How do you read user input in a script?

bash
echo "Enter your name: "
read name
echo "Hello, $name!"

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

bash
if [ -f "file.txt" ]; then
echo "File exists"
else
echo "File does not exist"
fi

7. How do you check if a directory exists?

bash
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?

bash
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?

bash
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?

bash
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?

bash
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?

bash
echo $?

(0 = success, non-zero = failure)

18. How do you redirect output to a file in shell scripting?

bash
echo "Hello" > output.txt

19. How do you append output to a file instead of overwriting?

bash
echo "New line" >> output.txt

20. How do you check if a command executed successfully?

bash
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?

bash
crontab -e

Example: Run script every day at 2 AM

bash
0 2 * * * /path/to/script.sh

23. How do you find the number of lines in a file?

bash
wc -l file.txt

24. How do you replace text in a file using sed?

bash
sed -i 's/old-text/new-text/g' file.txt

25. How do you filter output using grep?

bash
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?

bash
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?

bash
grep -o "word" file.txt | wc -l

30. What is a here document in shell scripting?

A here document allows multi-line input:

bash
cat << EOF
This is a multi-line text
EOF

31. How do you run a command in the background?

bash
long_running_command &

32. How do you create a log file for a script?

bash
./script.sh > script.log 2>&1

33. How do you kill a process using a script?

bash
kill -9 PID

34. How do you list open network connections?

bash
netstat -tulnp

35. How do you find the disk usage of a directory?

bash
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?

bash
ps -u username

Example:

bash
ps -u root

Shows all processes running under the root user.


37. How do you check the CPU and memory usage in Linux?

bash
top

or

bash
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

Primary Sidebar

Linux sysadmin tutorials linux system administrator

  • top 10 apt & apt-get commands (most used) apt vs apt-get
  • If-Else Statements in Shell Scripting
  • linux commands pdf (files & Directories, zip & unzip process, search etc)
  • Find Files with Specific Text on Linux grep find command
  • linux performance tuning inode limit file descriptors tco, kernel etc
  • Variables and Data Types in Shell Scripting
  • Top 10 most used Cat commands with examples (create, view, append files)
  • Ip tables / ufw / firewall d commands for block port ip rate limiting
  • Top 10 zip / tar commands to compress & extract files in linux
  • TOP 10 mv & cp commands in linux to move & copy files in Linux
  • Top 10 GREP Commands in linux to search files directory words strings
  • lsof netstat commands to know listening ports in linux 3 ways
  • Upgrade Ubuntu from 18.04 (19.10) to 20.04 LTS command line or gui server | desktop
  • 3 Ways (SCP, rsync, Sftp) linux server migration between two remote server apache nginx
  • linux system specs commands (CPU, Memory, Disk )speed, type. manufacture
  • linux sysctl command tweaks & hardening
  • linux security limits.conf deciding user limits process limits for nginx server
  • ulimit linux unlimited command unlimto set & know user limits open files file descriptor max user process etc.
  • red hat linux certification cost jobs salary syllabus courses fees
  • ufw firewall commads allow port enable disable ubuntu 20.04
  • ddos attack prevention
  • change ssh port in linux - avoid sshd ddos attacks
  • ping command
  • memcached install ubuntu wordpress
  • check linux version (lsb_release -a) ubuntu debian 32 or 64 bit
  • rsync command linux with examples comparison to scp
  • how to uninstall package in linux ubuntu rpm, yum apt-get
  • increase open file limit linux File descriptor ft nginx , mysql, lemp
  • remove repository ubuntu
  • htop commad memory details virtual vs shard vs resident
  • chown command in Linux with Examples
  • Kill PHP process
  • VIrtual Memory vs RSS Memory vs Shared memory in Linux
  • oom killer fixing it by configuration linux ubuntu
  • Install Lemp nginx mysql php fpm Stack on Debian 11 with repository
  • connect two remote servers linux command line
  • auto start after oom killer Mysql & php fpm nginx etc ubuntu wth systemd or cron job
  • load average Linux 1, 5, 15 min 2,4,8 cores explained
  • Control Structures in Shell Scripting
  • Shell Scripting Roadmap for Beginners to Advanced
  • awk commands with practical examples
  • Shell Scripting Tutorial for Beginners πŸš€
  • find Command in Linux with Examples
  • sed Command in Linux with Examples (Beginner to Advanced)
  • Linux Text processing commands in with Examples
  • linux disk management commands
  • fdisk command in linux with examples
  • how to add a new disk in linux
  • Linux mount Command with Examples
  • fstab options with examples
  • Top 50 Shell Scripting Interview Questions and Answers
  • Linux Networking Interview Questions and Answers
  • Linux Networking Commands Cheat Sheet with Examples pdf
  • Netstat & SS Commands cheat sheet with examples Interview Questions
  • Nmap Cheat Sheet – Network Scanning & Security
  • Bash Brackets ([], (), {}, $( ), $(( ))) – Types, Uses & Examples

hi i am raju ginni, primalry i manage wordpress websites on GCP cloud platform as a cloud engineer, and create content on passionate things.
you can follow me on youtbe

© 2025 - All Rights Reserved Disclaimer & Privacy Policy