Shell Path Quoting for Spaces

lesson tools active tools/shell-path-quoting.md View on GitHub

Shell Path Quoting for Spaces

Rule

Always quote paths that may contain spaces in shell commands.

Context

When constructing shell commands with file paths, especially user-provided paths or paths with spaces.

Detection

Observable signals that you need proper quoting:

Pattern

# Wrong: unquoted path with spaces
cd /path with spaces  # Error: too many arguments

# Correct: quoted path
cd "/path with spaces"

# Wrong: unquoted variable expansion
cd $PROJECT_PATH  # Breaks if path has spaces

# Correct: quoted variable
cd "$PROJECT_PATH"

# Wrong: unquoted command substitution
cd $(get_project_path)  # Breaks if returned path has spaces

# Correct: quoted command substitution
cd "$(get_project_path)"

Outcome

Following this pattern prevents:

Results in:

Related

Match Keywords

path with spaces quoted path spaces quoting cd: too many arguments too many arguments unquoted file path