ollama-code

Shell Tool (run_shell_command)

This document describes the run_shell_command tool for Ollama Code.

Description

Use run_shell_command to interact with the underlying system, run scripts, or perform command-line operations. run_shell_command executes a given shell command, including interactive commands that require user input (e.g., vim, git rebase -i) if the tools.shell.enableInteractiveShell setting is set to true.

On Windows, commands are executed with cmd.exe /c. On other platforms, they are executed with bash -c.

Arguments

run_shell_command takes the following arguments:

How to use run_shell_command with Ollama Code

When using run_shell_command, the command is executed as a subprocess. You can control whether commands run in background or foreground using the is_background parameter, or by explicitly adding & to commands. The tool returns detailed information about the execution, including:

Required Background Parameter

The is_background parameter is required for all command executions. This design ensures that the LLM (and users) must explicitly decide whether each command should run in the background or foreground, promoting intentional and predictable command execution behavior. By making this parameter mandatory, we avoid unintended fallback to foreground execution, which could block subsequent operations when dealing with long-running processes.

Background vs Foreground Execution

The tool intelligently handles background and foreground execution based on your explicit choice:

Use background execution (is_background: true) for:

Use foreground execution (is_background: false) for:

Execution Information

The tool returns detailed information about the execution, including:

Usage:

run_shell_command(command="Your commands.", description="Your description of the command.", directory="Your execution directory.", is_background=false)

Note: The is_background parameter is required and must be explicitly specified for every command execution.

run_shell_command examples

List files in the current directory:

run_shell_command(command="ls -la", is_background=false)

Run a script in a specific directory:

run_shell_command(command="./my_script.sh", directory="scripts", description="Run my custom script", is_background=false)

Start a background development server (recommended approach):

run_shell_command(command="npm run dev", description="Start development server in background", is_background=true)

Start a background server (alternative with explicit &):

run_shell_command(command="npm run dev &", description="Start development server in background", is_background=false)

Run a build command in foreground:

run_shell_command(command="npm run build", description="Build the project", is_background=false)

Start multiple background services:

run_shell_command(command="docker-compose up", description="Start all services", is_background=true)

Configuration

You can configure the behavior of the run_shell_command tool by modifying your settings.json file or by using the /settings command in the Ollama Code.

Enabling Interactive Commands

To enable interactive commands, you need to set the tools.shell.enableInteractiveShell setting to true. This will use node-pty for shell command execution, which allows for interactive sessions. If node-pty is not available, it will fall back to the child_process implementation, which does not support interactive commands.

Example settings.json:

{
  "tools": {
    "shell": {
      "enableInteractiveShell": true
    }
  }
}

Showing Color in Output

To show color in the shell output, you need to set the tools.shell.showColor setting to true. Note: This setting only applies when tools.shell.enableInteractiveShell is enabled.

Example settings.json:

{
  "tools": {
    "shell": {
      "showColor": true
    }
  }
}

Setting the Pager

You can set a custom pager for the shell output by setting the tools.shell.pager setting. The default pager is cat. Note: This setting only applies when tools.shell.enableInteractiveShell is enabled.

Example settings.json:

{
  "tools": {
    "shell": {
      "pager": "less"
    }
  }
}

Interactive Commands

The run_shell_command tool now supports interactive commands by integrating a pseudo-terminal (pty). This allows you to run commands that require real-time user input, such as text editors (vim, nano), terminal-based UIs (htop), and interactive version control operations (git rebase -i).

When an interactive command is running, you can send input to it from the Ollama Code. To focus on the interactive shell, press ctrl+f. The terminal output, including complex TUIs, will be rendered correctly.

Important notes

Environment Variables

When run_shell_command executes a command, it sets the OLLAMA_CODE=1 environment variable in the subprocess’s environment. This allows scripts or tools to detect if they are being run from within the CLI.

Command Restrictions

You can restrict the commands that can be executed by the run_shell_command tool by using the tools.core and tools.exclude settings in your configuration file.

The validation logic is designed to be secure and flexible:

  1. Command Chaining Disabled: The tool automatically splits commands chained with &&, ||, or ; and validates each part separately. If any part of the chain is disallowed, the entire command is blocked.
  2. Prefix Matching: The tool uses prefix matching. For example, if you allow git, you can run git status or git log.
  3. Blocklist Precedence: The tools.exclude list is always checked first. If a command matches a blocked prefix, it will be denied, even if it also matches an allowed prefix in tools.core.

Command Restriction Examples

Allow only specific command prefixes

To allow only git and npm commands, and block all others:

{
  "tools": {
    "core": ["run_shell_command(git)", "run_shell_command(npm)"]
  }
}

Block specific command prefixes

To block rm and allow all other commands:

{
  "tools": {
    "core": ["run_shell_command"],
    "exclude": ["run_shell_command(rm)"]
  }
}

Blocklist takes precedence

If a command prefix is in both tools.core and tools.exclude, it will be blocked.

{
  "tools": {
    "core": ["run_shell_command(git)"],
    "exclude": ["run_shell_command(git push)"]
  }
}

Block all shell commands

To block all shell commands, add the run_shell_command wildcard to tools.exclude:

{
  "tools": {
    "exclude": ["run_shell_command"]
  }
}

Security Note for excludeTools

Command-specific restrictions in excludeTools for run_shell_command are based on simple string matching and can be easily bypassed. This feature is not a security mechanism and should not be relied upon to safely execute untrusted code. It is recommended to use coreTools to explicitly select commands that can be executed.