Advanced Questions¶
How to use these interview questions
๐ง Read each question carefully.
Try answering it yourself before expanding the answer to compare with the ideal response.
Level: Advanced
๐ด Complex Scenarios & Architecture.
Tackle high-level design, production-grade scaling, security, and advanced internals.
1. How do you define a function in Bash?
function_name() {
# commands
}
function keyword:
function function_name {
# commands
}
2. How do you pass arguments to a function?
Just like a script: function_name arg1 arg2.
Inside the function, access them using $1, $2, etc.
3. How do you return a value from a function?
Using return exit_code (0-255) or echo for strings.
return: Only returns an integer status code (captured by$?).echo: Prints output which can be captured via substitutionresult=$(my_function).
4. What is the scope of variables in a function?
Global by default.
Use the local keyword to make a variable restricted to the function:
local var_name="value"
5. How do you process text using awk?
awk is used for pattern scanning and processing.
Example: Print the first column of a file:
awk '{print $1}' file.txt
6. How do you replace text using sed?
sed 's/old/new/g' filename.
s: Substituteg: Global (replace all occurrences)
7. How do you find a line number containing a specific string?
grep -n "string" filename.
8. How do you handle zombie processes using scripts?
Find them using ps aux | grep 'Z' and kill the parent process if necessary.
9. How do you run a script in the background?
Append & at the end.
Example: ./script.sh &.
10. What is nohup?
No Hang Up.
It allows a command to keep running even after you log out: nohup ./script.sh &.
11. How do you schedule a script to run periodically?
Using cron.
Edit crontab with crontab -e and add entry:
* * * * * /path/to/script.sh
12. How do you check if a port is open?
nc -zv host port.
Example: nc -zv localhost 80.
13. How do you redirect both stdout and stderr to the same file?
./script.sh > log.txt 2>&1.
14. What is eval command?
It takes arguments and executes them as a command. It creates a "second pass" of parsing.
15. How do you debug scripts with bash -x?
It prints each command before executing it, expanding variables. Useful for tracing logic errors.
16. How do you create a temporary file securely?
mktemp.
Example: temp_file=$(mktemp).
17. How do you parse JSON in shell scripts?
Using jq.
Example: cat data.json | jq '.key'.
18. What is xargs used for?
To build and execute command lines from standard input.
Example: find . -name "*.log" | xargs rm.
19. How do you check if a variable is empty?
if [ -z "$var" ]; then ....
20. How do you get unique lines from a file?
sort filename | uniq.
๐งช Ready to test yourself?¶
๐ Take the Advanced Shell Scripting Quiz (Coming Soon)
๐ฌ DevopsPilot Weekly โ Learn DevOps, Cloud & Gen AI the simple way.
๐ Subscribe here