Skip to content
Latchkey

Shell word splitting and globbing of an unquoted variable in CI

When you leave a variable unquoted, the shell splits its value on $IFS (spaces, tabs, newlines) into separate words and then expands any *, ?, or [ as a filename glob. A path with a space, or a value like *, becomes multiple arguments or an unexpected file list.

What this error means

A command receives the wrong arguments in CI: a path with a space is treated as two files, or a variable holding * expands to every file in the directory. Works when values have no spaces or globs, fails when they do.

bash
# DIR="/tmp/my logs"
rm -rf $DIR          # runs: rm -rf /tmp/my logs  (two args)
# PATTERN="*"
echo count: $PATTERN  # expands to every filename

Common causes

Whitespace triggers word splitting

An unquoted expansion is split on IFS, so "$DIR" with a space becomes two arguments and the command acts on the wrong targets.

Glob characters trigger pathname expansion

An unquoted value containing *, ?, or [...] is expanded against the filesystem, replacing the intended literal with matching filenames.

How to fix it

Double-quote every expansion

Quoting suppresses both splitting and globbing, so the value is passed as a single literal argument.

bash
rm -rf "$DIR"
echo "count: $PATTERN"

Use arrays for lists in bash

When you truly need multiple arguments, store them in an array and expand with "${arr[@]}" so each element stays intact.

bash
files=("a b.txt" "c.txt")
cp "${files[@]}" dest/

How to prevent it

  • Quote all variable and command-substitution expansions by default.
  • Use "${arr[@]}" for lists instead of unquoted strings.
  • Run shellcheck; SC2086 flags unquoted expansions.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →