This revised guide turns the original long Q&A into a practical reference for engineers preparing for Bash and UNIX interviews. It focuses on topics that show up in real DevOps work: scripting, command execution, redirection, permissions, and process control. Original post: Shell Bash Scripting Interview Questions and Answer – Part 1.
Originally published on 2010-04-16; fully revised and updated.

What shell scripting is and why it matters
Shell scripting means writing command-line instructions in a text file and running them through a shell interpreter such as bash, sh, or ksh. It is primarily used for automation on UNIX-like systems, and it often acts as glue between OS tools, CI jobs, and deployment scripts.
- Automates repetitive admin tasks.
- Combines existing tools quickly without compiling code.
- Works well for bootstrapping, CI/CD steps, and operational runbooks.
- Can be risky when scripts are unquoted or run with destructive commands.
How a shell command executes under the hood
Many interview questions around fork, exec, return codes, and zombies become easier once you understand the command lifecycle.
flowchart TD
A[User enters command] --> B[Shell parses tokens and expansions]
B --> C{Builtin}
C -- yes --> D[Run builtin in current shell]
C -- no --> E[Fork child process]
E --> F[Exec program in child]
F --> G[Program exits with status]
D --> H[Shell records exit code]
G --> H
H --> I[Prompt returns]
If the command is an external program, the shell usually creates a child process and then executes that program in the child. The exit code is returned to the shell, which makes it available as $?.
Essential interview topics with practical command examples
Exit codes and command chaining
By convention, exit code 0 means success and non-zero means failure. Operators like && and || chain commands based on that status.
#!/usr/bin/env bash
grep -q "ready" app.log
echo "Last exit code: $?"
grep -q "ready" app.log && echo "Service is ready" || echo "Service is not ready"Code language: PHP (php)
Stdin, stdout, and stderr (file descriptors 0, 1, 2)
Standard input, output, and error are core to shell pipelines and logging strategy.
my_command >stdout.log 2>stderr.log
my_command >combined.log 2>&1
my_command <input.txtCode language: CSS (css)
Executing a script vs sourcing a script
| Method | Typical syntax | Runs in current shell | Can change current shell environment |
|---|---|---|---|
| Execute script | ./script.sh | No | No |
| Source script | source ./script.sh or . ./script.sh | Yes | Yes |
# env-demo.sh
export DEMO_VALUE="set-by-script"
./env-demo.sh
echo "$DEMO_VALUE" # usually empty in parent shell
source ./env-demo.sh
echo "$DEMO_VALUE" # now visible in current shellCode language: PHP (php)
Shebang and interpreter selection
The shebang is the first line of an executable script and selects the interpreter.
#!/usr/bin/env bash
set -euo pipefail
echo "Running with Bash $BASH_VERSION"Code language: PHP (php)
Permissions, links, and file access fundamentals
Interviewers frequently ask how to set permissions and how links behave in UNIX filesystems.
chmod 664 myfile
chmod u+rwx,g+rx,o-rwx deploy.sh
ls -l deploy.shCode language: CSS (css)
| Link type | What it references | Cross-filesystem support | Typical command |
|---|---|---|---|
| Hard link | Same inode as target file | No | ln file1 file2 |
| Symbolic link | Path to another file or directory | Yes | ln -s file1 link1 |
Command comparisons often asked in interviews
| Command | Comparison style | Best for | Example |
|---|---|---|---|
cmp | Byte-by-byte; reports first mismatch | Quick binary or exact byte comparison | cmp fileA fileB |
diff | Line-by-line differences | Text review and patch-style output | diff -u fileA fileB |
Process management, signals, daemons, and IPC
Core process questions usually center on process IDs, states, and signaling. A daemon is a background service process detached from an interactive terminal session.
sleep 300 &
BG_PID=$!
ps -p "$BG_PID" -o pid,ppid,stat,cmd
kill "$BG_PID" # graceful request
kill -9 "$BG_PID" # force kill if neededCode language: PHP (php)
Common inter-process communication (IPC) mechanisms include unnamed pipes, named pipes (FIFO), sockets, message queues, and shared memory.
mkfifo /tmp/demo.fifo
# terminal 1
cat /tmp/demo.fifo
# terminal 2
echo "hello via fifo" > /tmp/demo.fifoCode language: PHP (php)
Login shells, non-login shells, and environment variables
Startup files differ by shell implementation. The table below reflects common Bash behavior on Linux and UNIX-like systems.
| Session type | Common Bash startup files | Typical use |
|---|---|---|
| Login shell | /etc/profile, then one of ~/.bash_profile, ~/.bash_login, or ~/.profile | User environment initialization |
| Interactive non-login shell | ~/.bashrc | Aliases, prompt, interactive shell options |
| Non-interactive shell | May use BASH_ENV if set | Script-time environment setup |
echo "Login shell from env: $SHELL"
ps -p $$ -o comm=
shopt -q login_shell && echo "login shell" || echo "non-login shell"
export APP_ENV=prod
echo "$APP_ENV"
unset APP_ENVCode language: PHP (php)
Rapid-fire interview questions and concise answers
- What does
$?store? The exit status of the most recently executed command. - What are file descriptors 0, 1, and 2? Standard input, standard output, and standard error.
- Difference between
"$*"and"$@"?"$*"joins all positional parameters into one word;"$@"preserves each parameter as a separate word. - What is
IFS? The internal field separator used during word splitting. - What is a zombie process? A terminated child process whose parent has not yet collected its exit status with
wait. - What does
execdo in shell? Replaces the current shell process with the specified program. - How do you make a script executable? Add execute permission, for example
chmod +x script.sh.
Best practices and common pitfalls
- Quote variable expansions (for example
"$var") to avoid word-splitting bugs. - Check exit codes and fail fast for automation workflows.
- Use
set -euo pipefailin Bash scripts where appropriate. - Be careful with destructive commands like
rm, redirection, and wildcards. - Prefer explicit paths in cron/CI contexts where environment variables differ.
- Lint scripts with tools such as
shellcheckbefore production use.
A strong interview answer is not just a definition; it shows that you understand safe operational behavior, process control, and debugging in real shell automation.
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services — all in one place.
Explore Hospitals