From time to time, I’m involved into a trolling conversation when any linux kiddie tells me:
Bash is really the superior shell
I totally disagree, but as I’m getting older, I don’t argue anymore.
Anyway, in this post I will expose two arguments, or I should say two reasons, why I usually use
ksh93 to run my scripts.
Note I’m really talking about the engine of the script, (the shebang definition).
set I’m used to the bourn shell syntax therefore I also exclude any C shell from the comparison.
$SHELL for interactivity is
zsh because it’s efficient enough
and it has a bunch of really cool features I won’t discuss in this post (maybe later)
Read, loops, forks and efficiency…
More than 10 years ago, as I was working for a project at IBM, my excellent team leader told me to refer to this book: Unix Power Tools. I did learn a lot with it.
And one feature I’ve always used is the
while read loop.
The use case
Let’s take this script as example:
It simply iterate 500 times and display the counter on the screen.
The result of execution
Let’s execute it in different shells
Bash is the only one which does not display the expected result.
The explanation is that the shell sees a pipe and the fork the process. The assignation to the variable
a is in another context and therefore,
when the father wants to display
$a in the current shell, the variable is empty.
Wait, but why does
zsh) do display the correct result ?
Simply because ksh and zsh have noticed that the command after the pipe was a builtin, and therefore that it was un-useful to fork.
Strace to the rescue…
To prove it, let’s check for syscalls with the
strace tool, and count how many clones and calls are performed:
quod erat demonstrandum, twice as much clone in bash thant in ksh|zsh.
Of course this as an impact on performances, because fork are expensive, let’s query the execution time:
This sounds clear to me…
The KSH93 Getopts unknown feature
Another cool feature I’ve discovered recently is the little addon of the getopts feature.
I wanted to use the
getopts built in in a script. As usual, I did RTFM (because I never know when to use colon etc.).
Here is the extract of the man page of ksh93 relative to the getopts function:
This particular sentence, in the middle of the documentation peaked my interest
The option -? causes getopts to generate a usage message on standard error.
What? We can generate usage with getopts?
Cool, any script should be documented, but any documentation should not be difficult to implement.
An example is sometimes better than an explanation (and the book is complete on this subject)
Here are two singing examples of the usage output (sorry, I’m tired)
Ballad of a thin man
I’m gonna try with a little help (from my friends)
And let’s try with an invalid option…
By now, KSH93 remains my favorite engine for shell scripts, but is sometimes replaced by ZSH.
Actually, ZSH seems as “smart” and efficient, but this
getopts feature is really nice for any script aim to be distributed widely.