Member-only story
Top 15 Shell Script interview questions
2 min readDec 23, 2024
Here are the top 15 Shell Script interview questions that I always get whenever I have an interview:
1. How do you declare a variable in a shell script?
#!/bin/bash
VARIABLE_NAME="VARIABLE_VALUE"
channel="iamsangeetv"
2. How do you make a shell script executable?
chmod +x script.sh
./script.sh
3. How do you pass arguments to a shell script?
#!/bin/bash
echo "First argument: $1"
echo "Second argument: $2"
Usage:
./script.sh shellscript awesome
4. How do you check if a file exists in a shell script?
#!/bin/bash
if [ -e /var/www/html/index.html ]; then
echo "File exists"
else
echo "File does not exist"
fi
5. How do you write a loop that iterates over a list of items?
#!/bin/bash
for item in file1 file2 file3; do
echo $item
done
6. How can you redirect both stdout and stderr to a file?
ls /nonexistent &> output.log