Functional Shell: A Minimal Toolbox
A more functional approach to bash...
Join the DZone community and get the full member experience.Join For Free
I already wrote a post about adopting a functional programming style in bash scripts. Here I want to explore how to build a minimal, reusable functional toolbox for my bash scripts, avoiding redefinition of base functional bricks whenever I need them.
So, in short: I wish I could write a script (say
use-functional-bricks.sh) like the following:
referring to “globally” available functions
reduce(and maybe others, too) without to re-write them everywhere they are needed and without to be bound to external scripts invocation.
The way I think we can solve the problem refers to three interesting features available in bash:
- export functions from scripts (through
- execute scripts in the current shell’s environment, through the
- execute scripts when bash starts
So I wrote the following script (say
and added the following line at the end of my user’s
and… voila! now
reduce implemented in
functional-bricks.sh are available in all my bash sessions — so I can use them in all my scripts!
And because seeing is believing… if I launch the script
use-functional-bricks.shdefined above, I get the following output:
Published at DZone with permission of Pietro Martinelli. See the original article here.
Opinions expressed by DZone contributors are their own.