Skip to Main Content
bash Cookbook
book

bash Cookbook

by Carl Albing, JP Vossen, Cameron Newham
May 2007
Beginner content levelBeginner
628 pages
15h 46m
English
O'Reilly Media, Inc.
Content preview from bash Cookbook

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, ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Bash Quick Start Guide

Bash Quick Start Guide

Tom Ryder
bash Cookbook, 2nd Edition

bash Cookbook, 2nd Edition

Carl Albing, JP Vossen
Learning the bash Shell, Second Edition

Learning the bash Shell, Second Edition

Cameron Newham, Bill Rosenblatt
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 0596526784Supplemental ContentErrata Page