00:00:00One of the simplest ways of making your AI agents more powerful is by simply giving them
00:00:03access to bash, but that can be a little complex. You need a real shell, you need a file system,
00:00:09servers and containers, and overall just a lot of infrastructure, or at least you did.
00:00:13But what if I told you there's a simpler way? You can actually just use a typescript implementation
00:00:18of bash. Now I know that might sound a little bit crazy, but trust me this is super cool
00:00:22and it could even save you money. Let me show you how.
00:00:30This is a new package called justbash and you can see on that readme it says "simulated
00:00:34bash environment with an in-memory virtual file system written in typescript. This is
00:00:38designed for AI agents that need a secure, sandboxed bash environment."
00:00:42But before I show you how you can actually link this up to your AI agents to give them
00:00:45more capabilities, I just want to show you some really simple code so you have some context
00:00:48for how this works. Here I'm starting a new bash environment from the justbash package
00:00:53and inside of that we can run our bash commands. So we're doing env.exec and here I'm simply
00:00:57running the bash command of echo hello and saying I want to save that to a file called
00:01:01greeting.txt. Then on the next line I'm running an exec again in the same environment saying
00:01:06I now want to read that greeting.txt file. And you can see we can actually log out the
00:01:10standard output of the result that we get back there, the exit code and also the environment
00:01:14variables and when I run this you can see it has successfully written that file and also
00:01:18read it. We get an exit code of zero and then the environment variables in our simulated
00:01:22bash shell. But the important thing to remember here is as I said this is just a simulated
00:01:26bash shell, this is literally a version of those bash commands written in typescript.
00:01:30So it's not actually attached to any real shell, it's simply converting the command that we
00:01:34give it here into typescript and running that. So this file as well called greeting.txt that
00:01:39doesn't exist on any file system, that's actually an in-memory virtual file system although there
00:01:44are options if you do want to attach a real one. So a simple way of thinking about what
00:01:47it's doing is it's simply taking the bash command that we write inside of the exec function
00:01:51and converting that into a javascript function which it then runs which means that it doesn't
00:01:55need a real shell and we can actually see that in that codebase here. When I put echo inside
00:01:59of exec it's actually running this javascript function and we can see this simply has logic
00:02:03for passing the flags that the echo command has in bash and turning them into javascript
00:02:08and then simply printing out the output of whatever we put after echo and you can see
00:02:11down here it simply returns an object that has the standard output with the output in
00:02:15javascript standard error and also an exit code. And if you're thinking that it can't
00:02:19possibly handle all of the basics of bash you can actually see a list here of all of the
00:02:23supported commands and it really has loads of them it has things like cat, copy, orc,
00:02:27base64, it has advanced ones for data processing like jq, python 3 and sqlite and you can even
00:02:33run network requests as well using curl you actually set up a whitelist so it's actually
00:02:36secure and there's also shell features down here so it has pipes, redirections, command
00:02:41chaining it really just gives you everything that a basic bash shell would. So you can see
00:02:45the package is really cool and it's got a lot of functionality but you're also probably still
00:02:48wondering why you'd actually want a bash implementation in typescript and how this can help your agents
00:02:53and save you money. Well to answer that let me show you one of its use cases with a very
00:02:57simple chat app. Now say in this chat app i wanted to talk about this json file this is
00:03:02a json file that contains loads of records and let's say i wanted to ask the ai to retrieve
00:03:06certain information within this or maybe do some analysis on some of these fields you can
00:03:11see it's an absolutely massive file one of the simplest but definitely wrong ways of doing
00:03:15that is you could get the contents of the entire file and put it inside of the prompt you can
00:03:19see here i'm using the ai sdk and also using gpt 5.2. If we actually run this agent and
00:03:24ask it to fetch us a specific record you can see the assistant is going to reply and i can
00:03:28confirm this is correct as these larger models are pretty good at retrieving values from their
00:03:33large context but the trouble is this used 133,000 tokens and it's also going to start
00:03:39to fall apart if you ask it some more advanced questions maybe you want to do some data manipulation
00:03:43or ask it about data within certain ranges. As i said though that is obviously the wrong
00:03:48way to do things so the next way that you may have attempted before is maybe some rag
00:03:51method or even giving the agent a sandbox so it can run bash commands and manipulate the
00:03:56data that way but the trouble with those approaches is you start to need a lot of infrastructure
00:04:00so instead let's just simulate this with just bash. To do this with the ai sdk you can actually
00:04:05use the bash tool package which is built on top of just bash and what this allows us to
00:04:09do is in our api endpoint for the chat we can create a new bash tool passes any files that
00:04:13we want to be in our simulated bash environment so in my case i'm passing through that json
00:04:17file that we had earlier the really large one then i'm also setting up the destination for
00:04:21where these files are going to go so it's going to go into a simulated slash workspace directory
00:04:26after that all we need to do in the stream that we have from our ai sdk is pass it through
00:04:31the bash tool that we have so that's just bash tool dot bash and i've also passed it some
00:04:34agent instructions which you can find in the node modules folder these are simply just instructions
00:04:39that the package has provided that can help your ai agent understand how to use the bash
00:04:42tool now with that very simple setup if we ask the assistant the exact same question
00:04:46to fetch us a piece of information from that json file instead of using its own context
00:04:50and needing to do some needle in the haystack finding you can see it actually goes off and
00:04:54runs some bash commands but again this is in our simulated typescript environment in
00:04:58this case it tried to run a jq command but it looks like it ran into an error so next
00:05:02it ran ahead bash command so we could see what this file actually looked like and that way
00:05:06it got the correct format the jq command and gave us our answer so yes while we did get
00:05:10the exact same answer out of this and maybe it did take a little bit longer the key here
00:05:15is you can actually see it took six thousand input tokens whereas the other method took
00:05:19a hundred and thirty three thousand this is just a way better method for handling long
00:05:24context and that's not even its only advantage it becomes a lot more powerful when you start
00:05:28to ask more deep questions about your data for example if i ask it how many records between
00:05:33a thousand and two thousand five hundred have metadata dot active true you can see you can
00:05:37just simply go off and run a bash command and get us the answer straight away if you actually
00:05:41tried this in the other version a it would take up a lot of context and b it probably
00:05:45just give you a wrong answer based on guessing this is going to be far more accurate as you're
00:05:49essentially just saying to the agent hey you have complete access to a bash shell even though
00:05:53it technically doesn't it's honestly just such a simple free value add that you can provide
00:05:57to your agents to make them more powerful with no additional infrastructure needed and that's
00:06:01what i really love about it hopefully i've showcased just one of its use cases but trust
00:06:05me there is so many more as we actually saw earlier you can get basic python sql and curl
00:06:10commands working in here and if you wanted to you can actually attach this to a real file
00:06:14system let me know what you think of just bash in the comments below while you're there subscribe
00:06:18and as always see you in the next one