Advanced Bash Scripting
Good Practices applied
no op Commands
In this lesson, you’ll learn how to perform a “dry run” in your Bash or Unix shell scripts using the built-in no-op (:
) command. A dry run lets you verify script logic and flow without modifying files or data—perfect for testing complex workflows before production.
What Is a Dry Run?
The term “dry run” dates back to fire departments: they practice hose deployment without water, hence “dry.” In software, many tools offer a dry-run or no-op mode to preview changes safely.
Common Dry-Run Flags in DevOps Tools
Tool | Command Example | Dry-Run Flag |
---|---|---|
Ansible | ansible-playbook -i inventory playbook.yml --check | --check |
Kubernetes | kubectl apply -f deployment.yaml --dry-run=client -o yaml | --dry-run=client |
Puppet | puppet apply --noop my_manifest.pp | --noop |
Note
Some tools distinguish client-side vs server-side dry runs. Always check the official docs for supported modes and output formats.
Placeholder for Empty Branches: the :
Command
Leaving an if
or loop branch empty causes a Bash syntax error:
#!/usr/bin/env bash
if [[ "$1" = "start" ]]; then
else
echo "Invalid command."
fi
$ ./script.sh
script.sh: line 3: syntax error near unexpected token `else'
To satisfy Bash’s syntax without side effects, insert the no-op :
command:
#!/usr/bin/env bash
if [[ "$1" = "start" ]]; then
:
else
echo "Invalid command."
fi
Because :
is a shell builtin, it runs faster and cleaner than alternatives like echo ""
or true
.
Interpreter Errors vs. Runtime Errors
Interpreter errors occur at parse time—even if that code path never runs:
#!/usr/bin/env bash
if [[ "$1" = "start" ]]; then
# empty block → interpreter error
else
echo "Invalid command."
fi
Runtime errors only appear when execution reaches problematic code. For example, calling a non-existent command x
:
#!/usr/bin/env bash
if [[ "$1" = "start" ]]; then
x
else
echo "Invalid command."
fi
No arguments:
$ ./script2.sh Invalid command.
With
start
:$ ./script2.sh start ./script2.sh: line 4: x: command not found
Replacing x
with :
eliminates any error or output and exits cleanly:
#!/usr/bin/env bash
if [[ "$1" = "start" ]]; then
:
else
echo "Invalid command."
fi
$ ./script2.sh start
# no output, exit code 0
Warning
Don’t confuse :
with the external true
command—:
is built into the shell and more efficient.
References and Further Reading
For more on advanced Bash scripting patterns and best practices, visit the Advanced Bash-Scripting Guide.
Watch Video
Watch video content