We had the pleasure of having Caleb Cooper back with another interesting presentation. This time the focus was on BASH scripting. I think it might have been due to how different shell scripts looked compared to the web languages I'm usually working with, but I've never really even considered doing more complex things with shell scripting up until now. Thanks to Caleb's presentation I think that might change. Here are some of the features I found to be interesting.
- Arrays of both indexed and associative varieties.
- Brace Expansion allows you to use a simple and straightforward syntax to generate long and complex values without having to resort to loops and arrays.
- Cases. Case is the BASH equivalent to switches. The syntax is slightly different but the construct is generally the same as any other language. You create a case for each expected outcome of a condition and then react to it.
- Getopts is much like positional parameters in that it allows you to get command line options. The key difference is that getopts allows you to get command line options and process them regardless of order.
- Functions so you can just encapsulate blocks of operations and call them later.
- Math calculations. While BASH itself is limited to arithmetic on integers, external programs can be called to pick up the slack. eg. bc for floating point.
- Ternary Operators which can perform evaluations and set variables.
- Shellcheck is a BASH linter. While not a feature of BASH itself, it is still a useful tool to check your scripts before you accidentally do something catastrophic.
- Regex. BASH has the ability to perform regex without an external dependency.
A lot of these features are standard fare and represent a somewhat basic subset of what more robust programming languages offer. However, shell scripting doesn't need a lot of functionality. The features talked about in this presentation, especially when coupled with the output of all the command line tools available, can allow you to do incredibly complex things.
Having worked through the presentation slides, I've realized that BASH can be a powerful tool. A tool that is widely available and can be found on almost every platform. Heck even Windows has it now! Caleb really did a great job with this presentation. He definitely gets extra style points for writing the presentation software he used for the talk, in BASH! For anyone that hasn't really dug into shell scripting very much, I'd definitely recommend going through the slides. Links are below.