Bash
Execute custom Bash code within your AI workflows
Basic Usage
Bash Code Runner blocks allow you to execute Bash scripts within your Hunch canvas. This powerful feature enables you to process data, interact with the file system, use command-line tools, and create dynamic outputs.
Simple Example
Let’s start with a basic example. Assume we have a canvas with two input content blocks:
- A block titled “First Name”
- A block titled “Last Name”
We’ll create a Bash Code Runner block that combines these inputs to greet the person.
This script will output: “Hello [First Name] [Last Name]!” where [First Name] and [Last Name] are replaced with the actual contents of those input blocks.
Using Hunch CLI Tools
Hunch provides several CLI tools that you can use in your Bash scripts to interact with your canvas.
Key Commands
-
hunch compiled-prompt
- Outputs the compiled prompt from all upstream blocks.
- Example:
hunch compiled-prompt > /tmp/prompt.txt
-
hunch block
- Retrieves the content of a specific block.
- Example:
hunch block output --title "Block Title"
-
hunch input
- Outputs all input data for the Code Runner block in JSON format.
- Example:
hunch input > /tmp/input.json
Input Handling
Accessing Specific Blocks
To access the content of a specific block, use the hunch block
command:
Working with All Inputs
To work with all input blocks at once:
Output Handling
Text Output
Use redirection to /output.md
to send text output:
File Output
To output files, write them to the /output
directory:
Markdown Rendering
Output is rendered as Markdown. You can use Markdown syntax in your output:
Error Handling
It’s usually best not to handle errors. Rather let them propagate up to the Hunch platform so that the workflow stops. However, if you need to handle errors, you can use conditional statements:
Advanced Usage
Working with External Tools
Bash scripts can leverage a wide range of command-line tools. Here’s an example using curl
:
Data Transformation
Bash is powerful for text processing. Here’s a simple example using sed
and awk
:
Best Practices
- Modularity: Use functions to organize your code for better readability and reusability.
- Error Handling: Always include error handling to make your scripts more robust.
- Comments: Add comments to explain complex logic or non-obvious operations.
- Input Validation: Validate inputs to ensure your script can handle unexpected data.
- Quoting: Always quote your variables to prevent word splitting and globbing.
Conclusion
Bash Code Runner blocks offer a powerful way to extend the capabilities of your Hunch canvas. By combining Bash’s text processing capabilities and access to system tools with Hunch’s AI-driven workflows, you can create sophisticated, dynamic, and efficient applications.
Remember to leverage the power of command-line tools and always consider security implications when writing your scripts, especially when dealing with user inputs or sensitive data.