2025-12-02 03:38:48 +08:00
|
|
|
# Shell tool (`run_shell_command`)
|
2025-06-25 06:31:58 +08:00
|
|
|
|
|
|
|
|
This document describes the `run_shell_command` tool for the Gemini CLI.
|
|
|
|
|
|
|
|
|
|
## Description
|
|
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
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`.
|
2025-09-16 01:09:38 +08:00
|
|
|
|
2025-10-17 08:25:30 +08:00
|
|
|
On Windows, commands are executed with `powershell.exe -NoProfile -Command`
|
|
|
|
|
(unless you explicitly point `ComSpec` at another shell). On other platforms,
|
|
|
|
|
they are executed with `bash -c`.
|
2025-06-25 06:31:58 +08:00
|
|
|
|
|
|
|
|
### Arguments
|
|
|
|
|
|
|
|
|
|
`run_shell_command` takes the following arguments:
|
|
|
|
|
|
|
|
|
|
- `command` (string, required): The exact shell command to execute.
|
2025-10-09 20:17:37 +08:00
|
|
|
- `description` (string, optional): A brief description of the command's
|
|
|
|
|
purpose, which will be shown to the user.
|
|
|
|
|
- `directory` (string, optional): The directory (relative to the project root)
|
|
|
|
|
in which to execute the command. If not provided, the command runs in the
|
|
|
|
|
project root.
|
2025-06-25 06:31:58 +08:00
|
|
|
|
|
|
|
|
## How to use `run_shell_command` with the Gemini CLI
|
|
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
When using `run_shell_command`, the command is executed as a subprocess.
|
|
|
|
|
`run_shell_command` can start background processes using `&`. The tool returns
|
|
|
|
|
detailed information about the execution, including:
|
2025-06-25 06:31:58 +08:00
|
|
|
|
|
|
|
|
- `Command`: The command that was executed.
|
|
|
|
|
- `Directory`: The directory where the command was run.
|
|
|
|
|
- `Stdout`: Output from the standard output stream.
|
|
|
|
|
- `Stderr`: Output from the standard error stream.
|
|
|
|
|
- `Error`: Any error message reported by the subprocess.
|
|
|
|
|
- `Exit Code`: The exit code of the command.
|
|
|
|
|
- `Signal`: The signal number if the command was terminated by a signal.
|
|
|
|
|
- `Background PIDs`: A list of PIDs for any background processes started.
|
|
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
run_shell_command(command="Your commands.", description="Your description of the command.", directory="Your execution directory.")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## `run_shell_command` examples
|
|
|
|
|
|
|
|
|
|
List files in the current directory:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
run_shell_command(command="ls -la")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Run a script in a specific directory:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
run_shell_command(command="./my_script.sh", directory="scripts", description="Run my custom script")
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Start a background server:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
run_shell_command(command="npm run dev &", description="Start development server in background")
|
|
|
|
|
```
|
|
|
|
|
|
2025-09-16 01:09:38 +08:00
|
|
|
## Configuration
|
|
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
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 Gemini CLI.
|
2025-09-16 01:09:38 +08:00
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
### Enabling interactive commands
|
2025-09-16 01:09:38 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
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.
|
2025-09-16 01:09:38 +08:00
|
|
|
|
|
|
|
|
**Example `settings.json`:**
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"tools": {
|
2025-09-19 04:05:01 +08:00
|
|
|
"shell": {
|
|
|
|
|
"enableInteractiveShell": true
|
|
|
|
|
}
|
2025-09-16 01:09:38 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
### Showing color in output
|
2025-09-16 01:09:38 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
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.**
|
2025-09-16 01:09:38 +08:00
|
|
|
|
|
|
|
|
**Example `settings.json`:**
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"tools": {
|
|
|
|
|
"shell": {
|
|
|
|
|
"showColor": true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
### Setting the pager
|
2025-09-16 01:09:38 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
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.**
|
2025-09-16 01:09:38 +08:00
|
|
|
|
|
|
|
|
**Example `settings.json`:**
|
|
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
|
|
|
|
"tools": {
|
|
|
|
|
"shell": {
|
|
|
|
|
"pager": "less"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
## Interactive commands
|
2025-09-16 01:09:38 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
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`).
|
2025-09-16 01:09:38 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
When an interactive command is running, you can send input to it from the Gemini
|
2026-01-13 07:30:12 +08:00
|
|
|
CLI. To focus on the interactive shell, press `Tab`. The terminal output,
|
2025-10-09 20:17:37 +08:00
|
|
|
including complex TUIs, will be rendered correctly.
|
2025-09-16 01:09:38 +08:00
|
|
|
|
2025-06-25 06:31:58 +08:00
|
|
|
## Important notes
|
|
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
- **Security:** Be cautious when executing commands, especially those
|
|
|
|
|
constructed from user input, to prevent security vulnerabilities.
|
|
|
|
|
- **Error handling:** Check the `Stderr`, `Error`, and `Exit Code` fields to
|
|
|
|
|
determine if a command executed successfully.
|
|
|
|
|
- **Background processes:** When a command is run in the background with `&`,
|
|
|
|
|
the tool will return immediately and the process will continue to run in the
|
|
|
|
|
background. The `Background PIDs` field will contain the process ID of the
|
|
|
|
|
background process.
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
## Environment variables
|
2025-07-25 01:13:00 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
When `run_shell_command` executes a command, it sets the `GEMINI_CLI=1`
|
|
|
|
|
environment variable in the subprocess's environment. This allows scripts or
|
|
|
|
|
tools to detect if they are being run from within the Gemini CLI.
|
2025-07-25 01:13:00 +08:00
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
## Command restrictions
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
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.
|
|
|
|
|
|
|
|
|
|
- `tools.core`: To restrict `run_shell_command` to a specific set of commands,
|
|
|
|
|
add entries to the `core` list under the `tools` category in the format
|
|
|
|
|
`run_shell_command(<command>)`. For example,
|
|
|
|
|
`"tools": {"core": ["run_shell_command(git)"]}` will only allow `git`
|
|
|
|
|
commands. Including the generic `run_shell_command` acts as a wildcard,
|
|
|
|
|
allowing any command not explicitly blocked.
|
|
|
|
|
- `tools.exclude`: To block specific commands, add entries to the `exclude` list
|
|
|
|
|
under the `tools` category in the format `run_shell_command(<command>)`. For
|
|
|
|
|
example, `"tools": {"exclude": ["run_shell_command(rm)"]}` will block `rm`
|
|
|
|
|
commands.
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
The validation logic is designed to be secure and flexible:
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
1. **Command chaining disabled**: The tool automatically splits commands
|
2025-10-09 20:17:37 +08:00
|
|
|
chained with `&&`, `||`, or `;` and validates each part separately. If any
|
|
|
|
|
part of the chain is disallowed, the entire command is blocked.
|
2025-12-02 03:38:48 +08:00
|
|
|
2. **Prefix matching**: The tool uses prefix matching. For example, if you
|
2025-10-09 20:17:37 +08:00
|
|
|
allow `git`, you can run `git status` or `git log`.
|
2025-12-02 03:38:48 +08:00
|
|
|
3. **Blocklist precedence**: The `tools.exclude` list is always checked first.
|
2025-10-09 20:17:37 +08:00
|
|
|
If a command matches a blocked prefix, it will be denied, even if it also
|
|
|
|
|
matches an allowed prefix in `tools.core`.
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
### Command restriction examples
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
**Allow only specific command prefixes**
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
To allow only `git` and `npm` commands, and block all others:
|
2025-06-30 03:32:26 +08:00
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2025-08-29 06:31:33 +08:00
|
|
|
"tools": {
|
|
|
|
|
"core": ["run_shell_command(git)", "run_shell_command(npm)"]
|
|
|
|
|
}
|
2025-06-30 03:32:26 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
- `git status`: Allowed
|
2025-06-30 03:32:26 +08:00
|
|
|
- `npm install`: Allowed
|
2025-06-30 23:42:35 +08:00
|
|
|
- `ls -l`: Blocked
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
**Block specific command prefixes**
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
To block `rm` and allow all other commands:
|
2025-06-30 03:32:26 +08:00
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2025-08-29 06:31:33 +08:00
|
|
|
"tools": {
|
|
|
|
|
"core": ["run_shell_command"],
|
|
|
|
|
"exclude": ["run_shell_command(rm)"]
|
|
|
|
|
}
|
2025-06-30 03:32:26 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
- `rm -rf /`: Blocked
|
|
|
|
|
- `git status`: Allowed
|
2025-06-30 03:32:26 +08:00
|
|
|
- `npm install`: Allowed
|
|
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
**Blocklist takes precedence**
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
If a command prefix is in both `tools.core` and `tools.exclude`, it will be
|
|
|
|
|
blocked.
|
2025-06-30 03:32:26 +08:00
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2025-08-29 06:31:33 +08:00
|
|
|
"tools": {
|
|
|
|
|
"core": ["run_shell_command(git)"],
|
|
|
|
|
"exclude": ["run_shell_command(git push)"]
|
|
|
|
|
}
|
2025-06-30 03:32:26 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2025-06-30 23:42:35 +08:00
|
|
|
- `git push origin main`: Blocked
|
|
|
|
|
- `git status`: Allowed
|
2025-06-30 03:32:26 +08:00
|
|
|
|
|
|
|
|
**Block all shell commands**
|
|
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
To block all shell commands, add the `run_shell_command` wildcard to
|
|
|
|
|
`tools.exclude`:
|
2025-06-30 03:32:26 +08:00
|
|
|
|
|
|
|
|
```json
|
|
|
|
|
{
|
2025-08-29 06:31:33 +08:00
|
|
|
"tools": {
|
|
|
|
|
"exclude": ["run_shell_command"]
|
|
|
|
|
}
|
2025-06-30 03:32:26 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
- `ls -l`: Blocked
|
|
|
|
|
- `any other command`: Blocked
|
|
|
|
|
|
2025-12-02 03:38:48 +08:00
|
|
|
## Security note for `excludeTools`
|
2025-06-30 03:32:26 +08:00
|
|
|
|
2025-10-09 20:17:37 +08:00
|
|
|
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.
|