Bash REPL: The Complete Guide for DevOps Engineers
The bash REPL is where DevOps engineers spend most of their time. Understanding how to use it effectively separates productive engineers from those constantly fighting their tools.
This guide covers bash REPL fundamentals and advanced techniques. For capturing your REPL sessions as documentation, see how to save terminal commands.
What Is a Bash REPL?
REPL stands for Read-Eval-Print Loop. When you open a terminal, you’re in a bash REPL:
- Read: Bash reads your input
- Eval: Bash evaluates (executes) the command
- Print: Output is printed to the screen
- Loop: Bash waits for the next command
$ echo "Hello" # Read
Hello # Eval + Print
$ # Loop (waiting for next input)
Unlike scripts that run start-to-finish, the REPL lets you work interactively—exploring, testing, and building commands incrementally.
Why the Bash REPL Matters
The bash REPL is essential for:
- Debugging: Test commands before adding to scripts
- Exploration: Discover what’s in filesystems and APIs
- Incident response: Run commands interactively during outages
- Prototyping: Build complex pipelines step by step
Bash REPL Basics
Command Execution
# Simple command
ls -la
# Command with pipes
cat /var/log/syslog | grep error | tail -20
# Command substitution
echo "Today is $(date +%A)"
Variables in the REPL
# Set a variable
SERVER="production-1"
# Use it
ssh $SERVER
# View all variables
set | less
Command History
# View history
history
# Repeat last command
!!
# Repeat command by number
!42
# Search history
Ctrl+R
Interactive Features
The bash REPL provides powerful interactive capabilities.
Tab Completion
# Complete filenames
cat /etc/sys<TAB>
# → cat /etc/sysctl.conf
# Complete commands
system<TAB><TAB>
# Shows: systemctl, systemd-...
Brace Expansion
# Create multiple files
touch file{1,2,3}.txt
# Generate sequences
echo {a..z}
echo {1..10}
# Combine
mkdir -p project/{src,tests,docs}
Command Editing
# Edit current command in $EDITOR
Ctrl+X Ctrl+E
# Clear line
Ctrl+U
# Move by word
Alt+F (forward)
Alt+B (backward)
Advanced REPL Techniques
Process Substitution
Compare outputs without temp files:
diff <(ls dir1) <(ls dir2)
Here Documents
Multi-line input in the REPL:
cat << EOF
This is
multiple lines
of text
EOF
Here Strings
Single-line input:
grep "error" <<< "no error here"
Subshells
Isolate changes:
# Changes don't affect parent
(cd /tmp && rm -rf build_*)
# You're still in original directory
pwd
REPL for Debugging
The bash REPL excels at debugging.
Test Commands Safely
# See what would be deleted (don't actually delete)
find . -name "*.tmp" -print
# Only delete after confirming
find . -name "*.tmp" -delete
Build Pipelines Incrementally
# Step 1: Get data
kubectl get pods -o json
# Step 2: Filter (build on step 1)
kubectl get pods -o json | jq '.items[]'
# Step 3: Extract field
kubectl get pods -o json | jq '.items[].metadata.name'
# Step 4: Further processing
kubectl get pods -o json | jq -r '.items[].metadata.name' | sort
Debug Mode
# Enable debug output
set -x
# Run commands (shows each step)
ls -la /tmp
# Disable debug output
set +x
REPL Session Management
Keep your REPL sessions organized.
Using tmux
# Start named session
tmux new -s incident-42
# Detach (keeps running)
Ctrl+B d
# Reattach later
tmux attach -t incident-42
# List sessions
tmux ls
Screen Alternative
# Start session
screen -S debugging
# Detach
Ctrl+A d
# Reattach
screen -r debugging
The REPL’s Limitation
The bash REPL is powerful for exploration. But REPL sessions are ephemeral:
- Commands scroll away
- Context is lost when you close the terminal
- Sharing requires copy-paste
- No record of what worked
From REPL to Runbook
Capture your REPL workflows as runbooks. Instead of remembering “that sequence of commands that fixed the database,” document it:
# Database Connection Recovery
## Check connection count
```bash
psql -c "SELECT count(*) FROM pg_stat_activity;"
```
## Terminate idle connections
```bash
psql -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle' AND query_start < now() - interval '1 hour';"
```
## Verify recovery
```bash
psql -c "SELECT state, count(*) FROM pg_stat_activity GROUP BY state;"
```
See our runbook examples for more templates.
Stew: REPL Meets Documentation
Stew gives you the interactivity of a bash REPL with the persistence of documentation.
Write commands in markdown. Run them with a click. See output inline. Share with your team.
Your REPL explorations become executable runbooks.
Join the waitlist and bridge the gap between exploration and documentation.