Uhh... dare I say that "maybe you're using the wrong (non-)language?"
No, no, I'm utterly and completely
serious!! (And I absolutely, positively, mean
nothing "
personal" in what I'm saying here, so please put that flame-torch away before ya hurt someone with it.)
Bash is a
shell, nothing more and nothing less. Yes, it is true that it does have a modicum of "scripting capability," but the only gentleman who ever felt that a shell was a proper place to put a complete programming language was
Dr. David Korn.
I respectfully disagree with Dr. Korn.
The Linux/Unix programming environments give you "an embarrassment of riches" with respect to programming and so-called "scripting" languages that were
specifically intended for the development of very sophisticated programs ... as well as the development of more utilitarian scripts.
All of them are available to you free-of-charge: Perl, Python, Ruby, Prolog, PHP, Haskell, Erlang,
aye, the list is endless. And
all of them can be used to construct "a command-line program" through the magic of the so-called "shebang" ...
#! ... appearing as the first line of your favorite script.
All shells will look at the first line of the file for a "shebang," and if they find it they will invoke the specified language-processor program to carry out the script ... and the user will be none the wiser.
Thus, you are liberated from your concerns of "how to make a Bash script do" ... what a Bash script was never intended to do. (And Bash is likewise liberated from any expectations of having to do what it was never intended to do.)
"HTH." (And let me reinforce, in closing, that I
am not poking fun at you. When "the proverbial little light-bulb came on" about this for
me, it was a
huge lateral shift in my thinking. I suddenly realized why a couple of Bell Labs researchers in the 1970's had completely blown MULTICS out of the water using nothing more than a PDP-7 and a different way of thinking about everything.)