Cover by Cameron Newham, JP Vossen, Carl Albing

Safari, the world’s most comprehensive technology and business learning platform.

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required

O'Reilly logo

Adding New Features to bash Using Loadable Built-ins

The material in this recipe also appears in Learning the bash Shell by Cameron Newham (O'Reilly).

Problem

You have something that you'd like bash to do, but there's no built-in command for it. For efficiency reasons, you want it to be built-in to the shell rather than an external program. Or, you already have the code in C and don't want to or can't rewrite it.

Solution

Use the dynamically loadable built-ins introduced in bash version 2.0. The bash archive contains a number of pre-written built-ins in the directory ./examples/ loadables/, especially the canonical hello.c. You can build them by uncommenting the lines in the file Makefile that are relevant to your system, and typing make. We'll take one of these built-ins, tty, and use it as a case study for built-ins in general.

The following is a list of the built-ins provided in bash version 3.2's ./examples/

basename.c

id.c

push.c

truefalse.c

cat.c

ln.c

realpath.c

tty.c

cut.c

logname.c

rmdir.c

uname.c

dirname.c

mkdir.c

sleep.c

unlink.c

finfo.c

necho.c

strftime.c

whoami.c

getconf.c

pathchk.c

sync.c

perl/bperl.c

head.c

print.c

tee.c

perl/iperl.c

hello.c

printenv.c

template.c

 

Discussion

On systems that support dynamic loading, you can write your own built-ins in C, compile them into shared objects, and load them at any time from within the shell with the enable built-in.

We will discuss briefly how to go about writing a built-in and loading it in bash. The discussion assumes that you have experience with writing, compiling, ...

Find the exact information you need to solve a problem on the fly, or go deeper to master the technologies and skills you need to succeed

Start Free Trial

No credit card required