✅ 1. What is the purpose of the Shebang (#!) in a shell script?
Answer:
The shebang (#!/bin/bash
) tells the system which interpreter to use to execute the script. For example, #!/bin/bash
runs the script with the Bash shell.
✅ 2. How do you define and use variables in shell scripting?
Answer:
Variables are defined using the syntax:
name="Engr Abhishek Roshan"
echo "Hello, $name"
No =
spacing and use $
to access values.
✅ 3. What is command substitution in Bash?
Answer:
It captures the output of a command and stores it in a variable:
today=$(date)
echo "Today is $today"
✅ 4. How do you read user input in a shell script?
Answer:
read -p "Enter your name: " username
echo "Welcome $username"
✅ 5. What are exit codes in shell scripting?
Answer:
Exit codes indicate command success or failure. 0
means success, anything else indicates an error. Use echo $?
to view the last command’s exit status.
✅ 6. How are conditional statements used in Bash scripts?
Answer:
if [ "$user" = "admin" ]; then
echo "Welcome admin"
else
echo "Access denied"
fi
✅ 7. What is the purpose of loops in shell scripting?
Answer:
Loops (for
, while
, until
) are used to repeat commands until a condition is met.
Example:
for i in {1..5}; do echo $i; done
✅ 8. How do you pass arguments to a shell script?
Answer:
Use positional parameters: $0
(script name), $1
, $2
, …, $#
(total args).
Example:
echo "First arg is $1"
✅ 9. What is the difference between single and double quotes in Bash?
Answer:
'single quotes'
– no variable expansion."double quotes"
– variables are expanded.
Example:
echo 'Hello $USER' # Prints as-is
echo "Hello $USER" # Prints username
✅ 10. How do you check if a file exists?
Answer:
if [ -e filename ]; then
echo "File exists"
fi
✅ 11. What is the case
statement in shell scripting?
Answer:
Used for pattern matching:
case $var in
start) echo "Starting...";;
stop) echo "Stopping...";;
esac
✅ 12. How do you iterate over a file line by line?
Answer:
while IFS= read -r line; do
echo "$line"
done < filename.txt
✅ 13. How to redirect output and errors in Bash?
Answer:
>
– overwrite stdout>>
– append stdout2>
– redirect stderr&>
– redirect both
Example:
command &>> logfile.txt
✅ 14. How do you define functions in shell scripts?
Answer:
function greet() {
echo "Hello $1"
}
greet Engr Abhishek Roshan
✅ 15. What is the export
command used for?
Answer:export
makes a variable available to child processes:
export PATH=$PATH:/my/custom/bin
✅ 16. How do you check file permissions in Bash?
Answer:
Use:
ls -l filename
stat filename
To check readability: [ -r filename ]
✅ 17. How to terminate a script on error?
Answer:
- Use
exit 1
- Use
set -e
to stop on any error - Use
trap
for cleanup before exit
✅ 18. What is the shift
command in Bash?
Answer:shift
discards the first positional argument and shifts the rest to the left, helpful in parsing command-line arguments.
✅ 19. How to perform arithmetic in Bash?
Answer:
a=5
b=10
sum=$((a + b))
echo $sum
✅ 20. How to find and replace text in a file using sed?
Answer:
sed -i 's/old/new/g' file.txt