Bash/Linux Interview Questions for DevOps Engineers
Last updated
Last updated
Bash scripting is a superpower for engineers. Whether automating repetitive tasks, gluing together tools, or managing systems, Bash is always there, simple yet powerful.
1. What is the purpose of the shebang (#!
) line?
It defines the interpreter that should execute the script. Example:
2. How do you read command-line arguments in a shell script?
3. What are the standard streams in Linux?
Standard Input (stdin
)
Standard Output (stdout
)
Standard Error (stderr
)
4. What are the various types of variables used in shell scripting?
There are two types of variables used in shell scripting:
System-generated variables: These are created by the OS and can be viewed using the set
command.
User-defined variables: Created by users and accessed using the echo
command.
5. How do you check if a file exists using a conditional statement?
6. How do you find and delete files older than 7 days?
7. What is crontab
?
crontab
(CRON Table) is used to schedule jobs to run at specific times. It allows automation of system tasks.
8. How do you debug a shell script?
Use set -x
to enable debugging.
Use echo
statements to track variable values and execution flow.
9. How do you rename all .txt
files to .log
in a directory?
10. What are the default permissions of a file when it is created?
The default permissions are determined by the umask
value. Typically, a new file has rw-rw-r--
(664) permissions.
11. What are the four stages of a Linux process?
Waiting: Process is waiting for a resource.
Running: Process is currently executing.
Stopped: Process execution has been stopped.
Zombie: Process has finished but still exists in the process table.
12. What is a metacharacter?
A metacharacter is a special character in the shell used for pattern matching. Example:
Lists all files starting with ‘s’.
13. Difference between $@
and $*
?
$*
treats all arguments as a single string.
$@
treats each argument as a separate entity.
14. How would you check if a file named example
exists in a specific path?
You can also use:
15. How would you check disk usage using shell commands?
df -h
: Shows available disk space.
du -sh /data01/*
: Shows disk usage per directory.
dfspace
: Provides space usage in megabytes.
16. What is the special shell variable $?
?
It stores the exit status of the last executed command.
17. How to extract a substring from a variable?
18. Difference between [[ $string == "efg*" ]]
and [[ $string == efg* ]]
?
[[ $string == efg* ]]
checks if $string
starts with "efg".
[[ $string == "efg*" ]]
checks if $string
is exactly "efg*".
19. How to list only filenames in a directory?
To store the output in a variable:
20. What is the difference between soft and hard links?
Soft link (symbolic link): A pointer to the original file.
Hard link: A duplicate entry pointing to the same inode as the original file.
21. How do you replace a string in multiple files using sed
?
22. What does grep -oP '\\d+'
do?
It extracts numeric values from a string.
Output: 201963
These questions cover key Bash and Linux concepts that every DevOps engineer should be familiar with. Mastering these will help you ace your next interview!
Happy scripting! 🖥️