Hook execution fails with exit code 127 when project path contains spaces
Bug Description
Claude Code hook execution fails when the project directory path contains
spaces, resulting in "command not found" errors (exit code 127) during
hook execution.
Environment
- OS: Linux (Ubuntu-based)
- Claude Code Version: Latest
- Hook Event: SessionStart (but affects all hook types)
Problem Details
When CLAUDE_PROJECT_DIR contains spaces, Claude Code's hook execution
engine doesn't properly quote the path when executing hook scripts,
causing shell parsing errors.
Example Scenario
- Project Path: /home/user/My Projects/test-project/
- Hook Command: $CLAUDE_PROJECT_DIR/.claude/scripts/example_hook.sh
- Shell Execution: Shell tries to execute /home/user/My as a command with
arguments Projects/test-project/.claude/scripts/example_hook.sh
Steps to Reproduce
- Create a Claude Code project in a directory path containing spaces:
mkdir -p "/home/user/Test Project/claude-hooks-test"
cd "/home/user/Test Project/claude-hooks-test"
- Initialize Claude Code project with a simple hook:
{
"hooks": [
{
"event": "SessionStart",
"matcher": {
"all": true
},
"command": "$CLAUDE_PROJECT_DIR/.claude/scripts/test_hook.sh",
"timeoutMs": 5000,
"blocking": false
}
]
}
- Create the hook script:
mkdir -p .claude/scripts
echo '#!/bin/bash\necho "Hook executed successfully"' >
.claude/scripts/test_hook.sh
chmod +x .claude/scripts/test_hook.sh
- Start Claude Code session
- Observe hook failure in logs
Expected Behavior
Hook should execute successfully regardless of spaces in project path.
Actual Behavior
SessionStart:startup
[$CLAUDE_PROJECT_DIR/.claude/scripts/test_hook.sh] failed
with non-blocking status code 127: /bin/sh: 1:
/home/user/Test: not found
Root Cause Analysis
The issue occurs because Claude Code's hook execution engine expands
$CLAUDE_PROJECT_DIR without proper shell quoting:
Current (broken) execution:
# Claude Code internally executes something like:
/home/user/Test Project/claude-hooks-test/.claude/scripts/test_hook.sh
Shell interprets this as:
- Command: /home/user/Test
- Arguments: Project/claude-hooks-test/.claude/scripts/test_hook.sh
Should be quoted as:
"/home/user/Test Project/claude-hooks-test/.claude/scripts/test_hook.sh"
Suggested Fix
In Claude Code's hook execution engine, ensure CLAUDE_PROJECT_DIR
expansion is properly quoted:
# Instead of:
$CLAUDE_PROJECT_DIR/.claude/scripts/hook.sh
# Use:
"$CLAUDE_PROJECT_DIR"/.claude/scripts/hook.sh
Workarounds
- Move project to path without spaces
- Use symbolic links:
ln -s "/home/user/Test Project/claude-hooks-test"
"/home/user/claude-test"
- Use environment variable substitution in hook scripts (though this
doesn't solve the root issue)
Impact
- Prevents Claude Code usage in projects with spaces in paths
- Common on Windows, macOS, and Linux desktop environments where default
directories often contain spaces
- Affects all hook types (SessionStart, PreToolUse, PostToolUse, etc.)
Additional Notes
This is a shell quoting issue in Claude Code's hook execution engine, not
in user-written hook scripts. User scripts that properly quote variables
work fine when the initial command execution succeeds.
---
Priority: Medium (affects usability for common directory naming patterns)
Labels: bug, hooks, shell-execution, path-handling
This issue has 6 comments on GitHub. Read the full discussion on GitHub ↗