Skip to main content
Open In ColabOpen on GitHub

Shell (bash)

Giving agents access to the shell is powerful (though risky outside a sandboxed environment).

The LLM can use it to execute any shell commands. A common use case for this is letting the LLM interact with your local file system.

Note: Shell tool does not work with Windows OS.

%pip install --upgrade --quiet langchain-community
from langchain_community.tools import ShellTool

shell_tool = ShellTool()
API Reference:ShellTool
print(shell_tool.run({"commands": ["echo 'Hello World!'", "time"]}))
Hello World!

real 0m0.000s
user 0m0.000s
sys 0m0.000s
``````output
/Users/wfh/code/lc/lckg/langchain/tools/shell/tool.py:34: UserWarning: The shell tool has no safeguards by default. Use at your own risk.
warnings.warn(

Use with Agentsโ€‹

As with all tools, these can be given to an agent to accomplish more complex tasks. Let's have the agent fetch some links from a web page.

from langgraph.prebuilt import create_react_agent

tools = [shell_tool]
agent = create_react_agent("openai:gpt-4.1-mini", tools)

input_message = {
"role": "user",
"content": (
"Download the README here and identify the link for LangChain tutorials: "
"https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md"
),
}

for step in agent.stream(
{"messages": [input_message]},
stream_mode="values",
):
step["messages"][-1].pretty_print()
API Reference:create_react_agent
================================ Human Message =================================

Download the README here and identify the link for LangChain tutorials: https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md
================================== Ai Message ==================================
Tool Calls:
terminal (call_mr86V0d6E9nQiJZT7Xw5fH0G)
Call ID: call_mr86V0d6E9nQiJZT7Xw5fH0G
Args:
commands: ['curl -o README.md https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md']
Executing command:
['curl -o README.md https://raw.githubusercontent.com/langchain-ai/langchain/master/README.md']
================================= Tool Message =================================
Name: terminal

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 5169 100 5169 0 0 114k 0 --:--:-- --:--:-- --:--:-- 114k
``````output
/langchain/libs/community/langchain_community/tools/shell/tool.py:33: UserWarning: The shell tool has no safeguards by default. Use at your own risk.
warnings.warn(
``````output
================================== Ai Message ==================================
Tool Calls:
terminal (call_LF8TGrgS84WvUvaazYnVfib8)
Call ID: call_LF8TGrgS84WvUvaazYnVfib8
Args:
commands: ["grep -i 'tutorial' README.md"]
Executing command:
["grep -i 'tutorial' README.md"]
================================= Tool Message =================================
Name: terminal

- [Tutorials](https://python.langchain.com/docs/tutorials/): Simple walkthroughs with
``````output
/langchain/libs/community/langchain_community/tools/shell/tool.py:33: UserWarning: The shell tool has no safeguards by default. Use at your own risk.
warnings.warn(
``````output
================================== Ai Message ==================================

The link for LangChain tutorials in the README is: https://python.langchain.com/docs/tutorials/

Was this page helpful?