Page Content

Tutorials

Most Commanly Asked Shell Scripting Examples For Interview

Shell Scripting Examples for Interview

Shell Scripting Examples For Interview
Shell Scripting Examples For Interview

Below are commonly asked Shell Scripting examples for interviews: These are practical, frequently asked, and suitable for freshers to experienced candidates.

1. Print “Hello World”

Script

#!/bin/bash
echo "Hello World"

Tests basic shell script structure and execution.

2. Read User Input

Script

#!/bin/bash
echo "Enter your name:"
read name
echo "Hello $name"

Checks knowledge of read and variables.

3. Add Two Numbers

Script

#!/bin/bash
echo "Enter first number:"
read a
echo "Enter second number:"
read b
sum=$((a + b))
echo "Sum is: $sum"

Tests arithmetic operations in shell.

4. Check Even or Odd Number

Script

#!/bin/bash
echo "Enter a number:"
read num

if [ $((num % 2)) -eq 0 ]; then
    echo "Even number"
else
    echo "Odd number"
fi

Checks conditional statements and arithmetic logic.

Also Read About Shell Script Security And Use Cases For Shell Scripting

5. Check File Exists or Not

Script

#!/bin/bash
echo "Enter file name:"
read file

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

Very common real-time interview question.

6. Print Numbers from 1 to 10

Script

#!/bin/bash
for i in {1..10}
do
    echo $i
done

Tests loop understanding.

7. Find Largest of Two Numbers

Script

#!/bin/bash
echo "Enter first number:"
read a
echo "Enter second number:"
read b

if [ $a -gt $b ]; then
    echo "$a is greater"
else
    echo "$b is greater"
fi

Checks comparison operators.

8. Count Number of Lines in a File

Script

#!/bin/bash
echo "Enter file name:"
read file
lines=$(wc -l < "$file")
echo "Number of lines: $lines"

Tests command usage and redirection.

9. Backup a Directory

Script

#!/bin/bash
src="/home/user/data"
dest="/home/user/backup/data_$(date +%F)"
cp -r $src $dest
echo "Backup completed"

Very common real-time shell scripting task.

10. Check Disk Usage

Script

#!/bin/bash
df -h

Tests system monitoring basics.

Also Read About Linux Shell Scripting For DevOps Interview Questions Guide

11. Print Command-Line Arguments

Script

#!/bin/bash
echo "Script name: $0"
echo "First argument: $1"
echo "Second argument: $2"
echo "Total arguments: $#"

Checks understanding of arguments.

12. Find Factorial of a Number

Script

#!/bin/bash
echo "Enter a number:"
read num
fact=1

for ((i=1; i<=num; i++))
do
    fact=$((fact * i))
done

echo "Factorial is $fact"

Tests loops and arithmetic logic.

13. Check Whether a Process Is Running

Script

#!/bin/bash
if pgrep sshd > /dev/null
then
    echo "sshd is running"
else
    echo "sshd is not running"
fi

Common Linux admin interview question.

14. Delete Files Older Than 7 Days

Script

#!/bin/bash
find /home/user/logs -type f -mtime +7 -delete
echo "Old files deleted"

Tests real-world maintenance scripting.

15. Simple Menu-Based Script

Script

#!/bin/bash
echo "1. Show date"
echo "2. Show calendar"
echo "3. Exit"
read choice

case $choice in
1) date ;;
2) cal ;;
3) exit ;;
*) echo "Invalid option" ;;
esac

Checks case statement usage.

Interview Tip (Very Important)

Interviewers expect you to:

  • Explain what each line does
  • Know why you used if, for, or case
  • Understand real-time usage, not just syntax

Most Asked Shell Scripting Topics in Interviews

  • Variables & input
  • If–else conditions
  • Loops
  • File handling
  • Command-line arguments
  • Backup & monitoring scripts

Also Read About Conditional Statements In Shell Scripting: If, Else & Case

Hemavathi
Hemavathihttps://govindhtech.com/
Myself Hemavathi graduated in 2018, working as Content writer at Govindtech Solutions. Passionate at Tech News & latest technologies. Desire to improve skills in Tech writing.
Index