FIND YOUR NEXT FEW LINES

The snippet library.

Small, practical examples. Search by task or narrow things down below.

10 snippets foundClear filters
Bash ScriptsDiagnostics & defensive scripting

Check a script’s syntax without executing its body

Bash Scripts: bash -n parses commands but does not run ordinary script statements.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Check for an installed command

Bash Scripts: command -v checks command resolution without assuming a specific installation path.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Check whether a function has been defined

Bash Scripts: declare -F tests a function name without executing it.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Check whether a variable exists under nounset

Bash Scripts: The plus expansion distinguishes an unset variable without reading its missing value.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Distinguish an empty input line from end of input

Bash Scripts: A successful read can return an empty value; its status indicates whether a line was read.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Identify whether a name resolves to a builtin

Bash Scripts: type -t reports the command kind without printing a machine-specific path.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Inspect every command status in a pipeline

Bash Scripts: Copy PIPESTATUS immediately before another command overwrites it.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Prevent a variable value from expanding as a filename pattern

Bash Scripts: Quote a variable to pass its literal contents even when matching filenames exist.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Refuse to overwrite an existing file with redirection

Bash Scripts: Noclobber makes a plain greater-than redirection fail when the target already exists.

Bash Scripts✓ Tested
Bash ScriptsDiagnostics & defensive scripting

Reject a missing required configuration value

Bash Scripts: Check a required value explicitly and emit a controlled diagnostic before exiting.

Bash Scripts✓ Tested