Skip to content
Vidar Holen edited this page Oct 26, 2026 · 7 revisions

Use "${var:?}" to ensure this never expands to /* .

Problematic code:

rm -rf "$STEAMROOT/"*

or

rm -rf /home/$(whoami)/*

Correct code:

rm -rf "${STEAMROOT:?}/"*

or

user=$(whoami)
rm -rf /home/${user:?}/*

Rationale:

If STEAMROOT is empty, this will end up deleting everything in the system's root directory.

Using :? will cause the command to fail if the variable is null or unset. Similarly, you can use :- to set a default value if applicable.

In the case of command substitution, assign to a variable first and then use :?. This is relevant even if the command seems simple and obviously correct, since forks and execs can fail due to external system limits and conditions, resulting in a blank substitution.

For more details about :? see the "Parameter Expansion" section of the Bash man page.

Exceptions:

None.

Clone this wiki locally

⌂ 换网址