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.

The image shows a computer monitor with a network or blockchain icon on the left and a series of colored lines resembling code on the right, with the text "No op command" at the top.

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.

The image shows two icons: a firefighter with a flame and a light bulb with a gear labeled "Wet run," under the title "No op command."

Common Dry-Run Flags in DevOps Tools

ToolCommand ExampleDry-Run Flag
Ansibleansible-playbook -i inventory playbook.yml --check--check
Kuberneteskubectl apply -f deployment.yaml --dry-run=client -o yaml--dry-run=client
Puppetpuppet 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

The image describes a "No-op command" as a placeholder shell-built command with no programmed behavior, and it includes references to looping and if-else statements.

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

Previous
Strict Mode