Using AI to Learn Bash shell scripting
January was a busy and interesting month. I spent a fair bit of time working with AI as my mentor/tutor to dig deeper into Bash shell scripting. I haven’t used AI for mentoring before and it was a pretty fun experience.
Bash is incredibly powerful, and you can combine it with Keyboard Maestro to create some powerful applets. It’s easy to make a UI in Keyboard Maestro to populate values, then call a shell script to accomplish some really impressive tasks.
To get the ball rolling, I started off asking for tutorials on different aspects of Bash scripting, such as “Can you give me a tutorial on
Strings in Bash scripting.” That would produce a short lesson on Strings with a few examples.
- Here’s how to create a string
- Here’s how to get the length of a string
- Here’s how to append text to a string
- Here’s how to concatenate strings
It gave me the basic usage, which was perfect. In those simple examples, it also provided some other topics I could look up.
Then, I moved on to practical examples of where and how to use various commands. “What command would I use if I wanted to …”
For example, “How I can find out the last time a file was accessed?”
This produced and example using
-atime with a breakdown of what the different parts of the command did or what each line did if it was a code block.
That turned into, “What is the correct syntax to…” I would get a usable snippet of code to accomplish the small task I asked for.
It was also helpful to have the AI break down solutions from online sources, like StackOverflow. Not everyone provides a complete explanation of their work, or not an explanation I can follow, so AI was able to give more detail so I could follow along.
Once I had a decent foundation, I started getting into the more common command line tools like,
sed. Echo can do a lot more than just output text to the screen.
That led into using the commands for actual apps, such as reading a file into an array and plenty of examples using String manipulation.
Just like other languages Bash has loops, conditionals, functions, arrays, and I have examples for all of them.
When putting it all together, I made some nifty examples for myself:
- A password generator
- Created a series of scripts to track look for Diablo 2
- A name generator using two files for input
- Worked with -atime to make a script to play music and movies older than a certain date based on their access time.
- A couple of test templates that use
catpopulate the text
I’ve asked AI plenty of general or research questions, but this was my first time using AI as an actual tutor and it was impressive. I can see why a lot of developers turn to AI. It’s much easier to ask a question directly than try and come up with the right search terms to match what you’re trying to do.
For its response, AI can produce a single command or a small block of text. It’s also capable of writing small, but complete programs.
You can explain what you want your program to do. You can then provide requirements and you should get a working app that is 98% accurate for what you want. The more detail you provide, the better the result.
For example, I have a text file with file names in it. I want to read the contents of that file and delete the listed files.
I gave AI the name and structure of the file, and some examples of real files I wanted to delete. It gave me a short little app that did exactly that.
There were a couple times where AI actually gave me some logic errors or it used a command I didn’t have available. It put some code in a less than optimal place, but when I pointed out the flaw, the AI refactored the code and corrected the mistake.
I was also impressed with the idea of pasting my code into AI and getting a fix for the error I was seeing or the wrong output. AI can certainly work as a debugger.
All the code examples I generated went into SnippetsLab, alongside my automation code using Katalon Studio and Groovy. My library went from 200 snippets to 500 and it’s still growing as I keep working and refining. Yeah, I copied down everything.
I’m still a beginner in shell scripting for sure, but I’ve learned a ton and now have a huge library of code examples to build from.
I’m still learning from AI and each week I come up with a new tool I can use to make myself more productive and efficient.