How to Use Bash with Kimi-CLI: Execute Shell Commands via CLI and Interactive Mode

Kimi-CLI provides a built-in Bash tool that lets you execute arbitrary shell commands either directly from the terminal via kimi bash "<command>" or within an interactive session using the /bash slash command.

MoonshotAI's kimi-cli repository ships with a native Bash tool integrated into its shell toolset. This feature allows you to run system commands safely through a subprocess wrapper, capturing output and exit codes while supporting asynchronous execution and optional approval workflows.

Running Bash Commands from the Command Line

You can execute one-off shell commands without entering interactive mode by using the kimi bash subcommand.

Basic CLI Syntax

Pass your command as a quoted string argument:


# List files in the current directory

kimi bash "ls -la"

# Check git branch status

kimi bash "git rev-parse --abbrev-ref HEAD"

# Run a complex pipe command

kimi bash "ps aux | grep python"

The CLI entry point is defined in src/kimi_cli/cli/__init__.py, which maps the bash subcommand to the tool execution logic. The command string is sent to the Bash tool implementation in src/kimi_cli/tools/shell/bash.md, which spawns a subprocess using Python's subprocess module and returns formatted output.

Using Bash in Interactive Mode

When running kimi in interactive mode, you can access the Bash tool through slash commands.

The /bash Slash Command

Inside the interactive shell UI (src/kimi_cli/ui/shell/), type /bash followed by your command:

>>> /bash uname -a
Linux myhost 5.15.0-76-generic #82-Ubuntu SMP x86_64 GNU/Linux

>>> /bash echo "Hello from Kimi!"
Hello from Kimi!

The UI parses the slash command and forwards it to the toolset loader in src/kimi_cli/soul/toolset.py, which invokes the Bash tool and streams the result back to the terminal while preserving line breaks and exit codes.

Programmatic Usage and Implementation

The Bash tool is implemented as a Python class that you can import for custom技能 (skills) or automation scripts.

Direct Tool Invocation

from kimi_cli.tools.shell.bash import BashTool

async def run_system_command():
    tool = BashTool()
    result = await tool.run(command="date")
    print(result.stdout)   # Output: Tue Jul 26 14:33:10 UTC 2026

    print(result.exit_code)  # Output: 0

The tool returns a KimiToolResult object containing stdout, stderr, and the exit status. This implementation resides in the shell tool directory and follows the same pattern as other shell-related tools like sh and powershell.

Safety and Runtime Features

The Bash tool includes several protective mechanisms and runtime integrations:

  • Safety checks: The tool validates that commands are non-empty strings and sanitizes potentially unsafe characters before execution.
  • Asynchronous execution: Commands run inside Kimi's asyncio-driven event loop, keeping the CLI responsive during long-running operations.
  • Approval workflows: If enabled in the session configuration, the Bash tool can require user approval before launching subprocesses.
  • Error handling: Non-zero exit statuses are formatted as tool-level failures and surfaced in the UI or logs.
  • Session logging: All command input and output are recorded in the session log directory (session/) for reproducible debugging.

Summary

  • Execute bash commands directly using kimi bash "<command>" from the terminal.
  • Use /bash <command> inside interactive mode for quick shell access.
  • The Bash tool source is located at src/kimi_cli/tools/shell/bash.md and loaded by src/kimi_cli/soul/toolset.py.
  • It uses Python's subprocess module with safety validations and returns structured KimiToolResult objects.
  • Supports async execution, approval workflows, and comprehensive session logging.

Frequently Asked Questions

How do I run a bash command directly from the terminal using kimi-cli?

Use the kimi bash subcommand followed by your command in quotes: kimi bash "echo Hello World". This bypasses interactive mode and returns the output immediately.

What is the difference between kimi bash and the /bash slash command?

kimi bash is a CLI flag used from your system terminal to execute one-off commands, while /bash is a slash command used within an interactive Kimi session (after typing kimi to enter the chat interface). Both invoke the same underlying tool but through different entry points.

Where is the Bash tool implementation located in the kimi-cli source code?

The Bash tool is defined in src/kimi_cli/tools/shell/bash.md and registered by the toolset loader in src/kimi_cli/soul/toolset.py. The CLI mapping is handled in src/kimi_cli/cli/__init__.py, and the interactive UI parsing happens in src/kimi_cli/ui/shell/.

Does the Bash tool support command approval workflows?

Yes. When enabled in the session configuration, the Bash tool can be gated by a user-approval step before the subprocess launches. This safety feature prevents accidental execution of dangerous commands and is part of the broader Shell tool family security model.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →