Bash loadable modules

September 14, 2008

programming

Or how to create new builtins for use in bash. Short answer: you can, but it is not enabled by default (at least on my distribution, Ubuntu)

This is also something that is done much better in zsh.

Why do want this? SPEED!

Shell scripting is a very easy way to program, but forking all these helper programs takes a lot of time. So one way to speed up your shell program is to load these programs into the shell and making them a builtin.

bash

I needed the bash source for this to work. So download it and compile it. Then in the examples/loadables you have some new builtins, like cat, cut, head and then some.

Making cat a builtin, you need the enable command

$ type cat
cat is /bin/cat

$ enable -f /tmp/bash-3.2/examples/loadables/cat cat
$ cat type
cat is a shell builtin

Same goes for other executables. When updating my site with nanoblogger, using cat and others as builtins made the update much quicker. I shaved off some 20 seconds on a total of 250 seconds; so it may be worth the effort.

zsh Here everything worked out of the box, but there is currently no cat builtin for zsh (I created one). But there are other interesting builtins.

What you need: man zshmodules and zmodload. With zmodload you can create new builtins.

For example, I made a cat builtin. Next I copy the cat.so to /usr/lib/zsh/4.3.4/zsh

% type cat
cat is /bin/cat

% zmodload zsh/cat
% type cat
cat is a shell builtin

Unload with zmodload -u. Works like a charm.

Nanoblogger