Shell Tool (run_shell_command)
The shell tool allows Qwen Code to execute system commands, run scripts, and perform command-line operations.
Overview
Tool Name:run_shell_command
Display Name: Shell
Kind: Other
Description: Executes shell commands on the underlying system. Supports both foreground and background execution with optional interactive mode.
Parameters
Usage
Basic Command
With Description
In Specific Directory
Background Execution
With Timeout
Background vs Foreground
Theis_background parameter is required and controls execution mode.
Foreground (is_background: false)
Use for:
- One-time commands:
ls,cat,grep - Build commands:
npm run build,make - Installation:
npm install,pip install - Git operations:
git commit,git push - Tests:
npm test,pytest
- Blocks until command completes
- Returns stdout, stderr, and exit code
- Default timeout: 120 seconds
- Can be customized with
timeoutparameter
Background (is_background: true)
Use for:
- Development servers:
npm run dev,npm start - Build watchers:
npm run watch,webpack --watch - Database servers:
mongod,redis-server - Web servers:
python -m http.server - Any long-running process
- Returns immediately
- Process continues running
- Returns process ID (PID)
- No timeout applied
- Adds
[background]indicator to description
Command Execution Details
Shell Environment
Platform-specific shells:- Windows:
cmd.exe /c - Unix/Linux/macOS:
bash -c
Working Directory
By default, commands run in the project root directory. Use thedirectory parameter for relative paths:
Output Handling
The tool returns:Interactive Commands
Enabling Interactive Shell
Set insettings.json:
Supported Interactive Commands
WithenableInteractiveShell enabled:
- Text editors:
vim,nano,emacs - Interactive tools:
htop,top - Version control:
git rebase -i,git add -p - Other TUIs: Any terminal-based UI
Usage
When an interactive command is running:- Press
Ctrl+Fto focus on the interactive shell - Interact normally with the command
- Exit the command normally (e.g.,
:qfor vim) - Control returns to Qwen Code
Configuration
Shell Settings
Command Restrictions
Restrict or block specific commands:Allowlist (Core Tools)
Allow only specific commands:git status✅ Allowednpm install✅ Allowedrm -rf /❌ Blocked
Blocklist (Exclude Tools)
Block specific commands:git status✅ Allowednpm install✅ Allowedrm file.txt❌ Blockedsudo apt update❌ Blocked
Blocklist Takes Precedence
If a command is in both lists, it’s blocked:git status✅ Allowedgit push❌ Blocked (more specific)
Command Chaining
Chained commands are validated separately:User Confirmation
Commands requiring user confirmation:Auto-Approved Commands
Safe, read-only operations:ls,cat,echogit status,git log,git diffnpm list- Custom allowlisted commands
Requires Confirmation
Potentially dangerous operations:- File modifications:
rm,mv,cp - System commands:
sudo,chmod,chown - Network:
curl,wget,ssh - Installation:
npm install,pip install - Custom blocklisted commands
Confirmation Dialog
When confirmation is required:Security Considerations
Path Validation
Working directories are validated:Command Injection
Be cautious with:- User-provided command strings
- Interpolated variables
- Shell metacharacters:
;,&&,||,|,>,<
Best Practices
-
Avoid user input in commands:
-
Use blocklist for dangerous commands:
-
Enable sandboxing:
-
Review confirmation prompts:
- Don’t blindly approve commands
- Understand what each command does
- Use “Never allow” for suspicious commands
Implementation
Location:packages/core/src/tools/shell.ts
Tool Class
Service
Location:packages/core/src/services/shellExecutionService.ts
Handles actual command execution:
Examples
Development Workflow
Git Workflow
Build and Deploy
Troubleshooting
Command Not Found
Error:command not found: mycommand
Solutions:
- Check if command is in PATH
- Use absolute path:
/usr/local/bin/mycommand - Check spelling and availability
Permission Denied
Error:Permission denied
Solutions:
- Check file permissions:
chmod +x script.sh - Run from correct directory
- Don’t use
sudo(blocked by default)
Timeout
Error:Command timed out
Solutions:
- Increase timeout:
timeout: 300000 - Use background execution:
is_background: true - Optimize slow command
Interactive Command Fails
Error:Input/output error
Solution:
Enable interactive shell:
Next Steps
- Task Tool - Delegate complex tasks
- File System Tools - File operations
- Tools System - Tools architecture
- Sandbox Tool - Sandboxed execution
