Thursday | 21 NOV 2024
[ previous ]
[ next ]

Thoughts on Bash

Title:
Date: 2021-04-04
Tags:  

After having written a few bash scripts that are relatively complex, I have some thoughts that I want to get down. The first is that arrays are quite strange in bash, the syntax, the delimiting, the way to reference the entire array is all different from regular languages. The internal field seperator is also a weird concept that is quite cool. Setting the IFS allows strings to be processed differently and it is a surprisingly intuitive way of thinking about things.

The most painful thing about writing bash is functions. Bash has functions but passing arguments is messy and returning things is done through global variables. This means that functions are helpful but just barely.

The great thing about bash however is that once you learn the syntax it all makes sense. I really like the idea of gluing things together and bash has that in spades. I have a rough idea of what I am trying to do and I can quickly start putting together various unix utilities and have something functional. This feels alot more ergonomical than something like python or rust.

The task I am most familiar with is to get a web page and grab specific pieces of information from that page. It is a simple task and I've done it countless times in a variety of languages. Rust, Python, and JavaScript all have packages that make it simple to get the web page and to parse it. Writing it in bash however is probably the quickest I've done it. All I need to do is curl the page and then use regex to grab what I need. This is in contrast to other languages where I need to remember the syntax to get the web page, then figure out which library to parse the html. It may be the fact that I don't use any of the regular languages day in day out but I also don't use bash that often either. In bash it intuitively flows which is maybe a function of unix tools in general. Unix has the idea of pipes and so that is baked into my understanding of how data flows in unix.

I've had quite a bit of fun writing bash scripts and getting something that works well gives me a sense of accomplishment that the other languages haven't. I've spent alot of time getting into rust and I like to believe that I'm at a point where I can quickly get something running in rust but it still feels very mechanical. Rust doesn't feel alive the same way bash does. Even compared to the other scripting languages, bash is something special with the way that you can invoke unix utilties. I would love for something to basically be bash but with proper functions, arrays and hashmaps. Allow for local variables by default and cut down the syntax for conditionals and loops would be a bonus. I think something like that would make a very fun language to program in.

For now however I'm going to continue using bash for anything I can think of, even if it might be a dumb as it makes me smile to know it's just a bunch of linux commands strung together to do something complex.