Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Shell Scripting Interview Questions, Explained: Bash Fundamentals, UNIX Processes, and Practical Commands

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.

shell-script-descriptive-interview-questions-sets

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

MethodTypical syntaxRuns in current shellCan change current shell environment
Execute script./script.shNoNo
Source scriptsource ./script.sh or . ./script.shYesYes
# 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 typeWhat it referencesCross-filesystem supportTypical command
Hard linkSame inode as target fileNoln file1 file2
Symbolic linkPath to another file or directoryYesln -s file1 link1

Command comparisons often asked in interviews

CommandComparison styleBest forExample
cmpByte-by-byte; reports first mismatchQuick binary or exact byte comparisoncmp fileA fileB
diffLine-by-line differencesText review and patch-style outputdiff -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 typeCommon Bash startup filesTypical use
Login shell/etc/profile, then one of ~/.bash_profile, ~/.bash_login, or ~/.profileUser environment initialization
Interactive non-login shell~/.bashrcAliases, prompt, interactive shell options
Non-interactive shellMay use BASH_ENV if setScript-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 exec do 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 pipefail in 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 shellcheck before 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.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
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.

Related Posts

TFS Interview Questions and Answers: Practical DevOps Guide

A structured, interview-focused guide to Team Foundation Server (TFS), including TFVC vs Git, build and reporting topics, and practical command examples. Use it to prepare concise, technically strong answers for common TFS interview scenarios.

Read More

Chef Interview Q&A (Part 2): Resources, Run-Lists, Bootstrap, and Test Kitchen

A practical rewrite of classic Chef interview questions, focused on how Chef resources converge, how run-lists drive node configuration, and how to test cookbooks safely. Includes command examples, workflow diagrams, and comparison tables.

Read More

Chef Interview Questions, Explained: Resources, Run-Lists, Bootstrapping, and Test Kitchen

This updated guide converts classic Chef interview Q&A into a practical reference for real infrastructure work. It covers core concepts, execution order, bootstrap flow, key knife commands, and Test Kitchen testing patterns.

Read More

SVN Interview Questions and Answers: Trunk, Branch, Tag, Update, Commit, Patch, Backup, and Authentication

A practical, interview-focused guide to Subversion (SVN) concepts and commands. Learn how to explain trunk/branch/tag, update vs commit, conflict handling, patching, backup/restore, and auth setup with confidence.

Read More

Jenkins Interview Questions, Answered: CI, APIs, Installation, Backups, and Essential Plugins

This revised guide turns common Jenkins interview prompts into practical answers you can explain with confidence. It covers CI fundamentals, API usage, setup on Ubuntu and RHEL-like systems, backup strategy, and key plugins.

Read More

Git, GitHub, and GitLab Interview Questions: Practical Answers and Command Guide

A practical interview-prep guide covering core Git concepts, essential commands, and common GitHub/GitLab questions. Includes workflow diagrams, comparisons, and troubleshooting checklists.

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x