Skip to main content

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

The is_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
Behavior:
  • Blocks until command completes
  • Returns stdout, stderr, and exit code
  • Default timeout: 120 seconds
  • Can be customized with timeout parameter

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
Behavior:
  • 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
Environment Variables:

Working Directory

By default, commands run in the project root directory. Use the directory parameter for relative paths:

Output Handling

The tool returns:

Interactive Commands

Enabling Interactive Shell

Set in settings.json:

Supported Interactive Commands

With enableInteractiveShell 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:
  1. Press Ctrl+F to focus on the interactive shell
  2. Interact normally with the command
  3. Exit the command normally (e.g., :q for vim)
  4. Control returns to Qwen Code

Configuration

Shell Settings

Command Restrictions

Restrict or block specific commands:

Allowlist (Core Tools)

Allow only specific commands:
Result:
  • git status ✅ Allowed
  • npm install ✅ Allowed
  • rm -rf / ❌ Blocked

Blocklist (Exclude Tools)

Block specific commands:
Result:
  • git status ✅ Allowed
  • npm install ✅ Allowed
  • rm file.txt ❌ Blocked
  • sudo apt update ❌ Blocked

Blocklist Takes Precedence

If a command is in both lists, it’s blocked:
Result:
  • git status ✅ Allowed
  • git push ❌ Blocked (more specific)

Command Chaining

Chained commands are validated separately:
Note: Command restrictions use simple prefix matching and are not a security mechanism. They help prevent accidental dangerous operations but should not be relied upon for security.

User Confirmation

Commands requiring user confirmation:

Auto-Approved Commands

Safe, read-only operations:
  • ls, cat, echo
  • git status, git log, git diff
  • npm 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:
Choosing “Always allow” adds the command to the allowlist for the session.

Security Considerations

Path Validation

Working directories are validated:

Command Injection

Be cautious with:
  • User-provided command strings
  • Interpolated variables
  • Shell metacharacters: ;, &&, ||, |, >, <

Best Practices

  1. Avoid user input in commands:
  2. Use blocklist for dangerous commands:
  3. Enable sandboxing:
  4. 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:
  1. Check if command is in PATH
  2. Use absolute path: /usr/local/bin/mycommand
  3. Check spelling and availability

Permission Denied

Error: Permission denied Solutions:
  1. Check file permissions: chmod +x script.sh
  2. Run from correct directory
  3. Don’t use sudo (blocked by default)

Timeout

Error: Command timed out Solutions:
  1. Increase timeout: timeout: 300000
  2. Use background execution: is_background: true
  3. Optimize slow command

Interactive Command Fails

Error: Input/output error Solution: Enable interactive shell:

Next Steps