[BUG] [PowerShell] Reporting $LASTEXITCODE causes unnecessary permission checks
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] I am using the latest version of Claude Code
What's Wrong?
I've given instructions to run scripts from the AGENTS.md and use the exit code to determine the response. When Claude calls these scripts it appends "; EXIT=$LASTEXITCODE" as a way to capture the exit code. This unfortunately halts work for a permission check, even when the called script is marked as allowed.
Here is an example tool use that will stop work for a permission check even though the script has been marked as allowed:
{ "type": "tool_use", "id": "toolu_01Wgvk8JU7LNCZrYp7Gs27b3", "name": "PowerShell", "input": { "command": "./scripts/check-device.ps1; \"EXIT=$LASTEXITCODE\"", "description": "Check device readiness" }
What Should Happen?
As an example, if a tool call is made for "./scripts/check-device.ps1; \"EXIT=$LASTEXITCODE\"" it should strip/ignore the last part and do the permission check against whats left "./scripts/check-device.ps1"
Error Messages/Logs
Steps to Reproduce
- Create a script "the-answer.ps1" with contents:
``` PowerShell
exit 42
2. Ask claude to run the script and tell what the exit code was.
3. Observe Claude asks for permissions to run a powershell script:
────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
PowerShell command
& .\the-answer.ps1; "Exit code: $LASTEXITCODE"
Run script and show exit code
Command contains expandable strings with embedded expressions
Do you want to proceed?
> 1. Yes
- Yes, and don’t ask again for: & .\exit42.ps1; "Exit code: $LASTEXITCODE"
- No
Esc to cancel · Tab to amend · ctrl+e to explain
4. Grant the permission and tell claude not to ask again what it suggests: & .\the-answer.ps1; "Exit code: $LASTEXITCODE"
5. Note the script runs and Claude successfully reports it returned 42.
6. Prompt Claude to run the script again, like "Please run the script again and tell me the exit code it returns this time."
7. Grant the permission and tell claude not to ask again for shorter & .\the-answer.ps1
8. Note the script runs and Claude successfully reports it returned 42.
9. Prompt Claude to run the script again, like "Please run the script again and tell me the exit code it returns this time."
10. Observe yet another permission check
For the expected results, there should not have been a second or third permission check. Let me record my settings.local.json after this interaction where it repeatedly asked for permissions:
```json
{
"permissions": {
"allow": [
"PowerShell(& .\\\\the-answer.ps1; \"Exit code: $LASTEXITCODE\")",
"PowerShell(& .\\\\the-answer.ps1)"
]
}
}
Claude Model
None
Is this a regression?
Yes, this worked in a previous version
Last Working Version
_No response_
Claude Code Version
2.1.168 (Claude Code)
Platform
Anthropic API
Operating System
Windows
Terminal/Shell
PowerShell
Additional Information
_No response_
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗