Blog Tags: 

Smack yourself if you don't use generic shell script hooks

Smack yourself in the forehead if you don't use the following snippet (or an equivalent) in all custom shell scripts that could benefit from a hooking mechanism:

    for script in $1/*; do

        # skip non-executable snippets
        [ -x "$script" ] || continue

        # execute $script in the context of the current shell
        . $script

run_scripts path/to/hooks.d

I've found this pattern useful in nearly every sufficiently complex shell script I maintain, and I even use it to help manage my bashrc and xsession configurations:

$ ls .bashrc.d/
paths git editor pager autologout qemu tmpdirs scratch

$ ls .xsession.d/
10-lang 10-tmpdir 90-bell 90-kbdrate 91-xscreensaver 99-fvwm-conf


  1. Modular: add or remove code without having to edit a big monolithic file.

  2. Order of execution is determined by the filename. Changing the order is as simple as changing a number prefix.

  3. Disable execution by removing execution bit:

    		chmod -x .bashrc.d/git

These days breaking down configurations into separate modular files like this is common in the Linux world, so by now I expect many experienced users are wondering why I'm channeling Captain Obvious. Just keep in mind that many Linux newbies haven't yet learned all our best practices and some lessons are worth reteaching.

Why not share your own tricks? Post a comment!


Ed Howland's picture

They look interesting:

paths CDPATHs?

git see [1] for mine

editor export EDITOR=vi

pager emport PAGER=less

autologout ??

qemu ??

tmpdirs ??

scratch ??

[1] My git functions:

function gvc { git commit -m "$*" }

function gam { git commit -am "$*" }

Liraz Siri's picture

Hi Ed! Scratch is where I put random shell functions or variables I write on-the-fly for the task at hand (e.g., a test case I don't want to retype over and over again).

It's all kind of boring and specific to my workload which is why I didn't share, but since you asked...

$ cat autologout
# logout from the console if no activity in 300 seconds
if [ -z "$DISPLAY" ]; then

$ cat qemu

# vde switch to connect to

# anything you put here is used as the seed for choosing the mac address

# examples: kvm qemu qemu-x86_64

function qemu() {
    if [ -n "$QEMU_VDE" ]; then
        if [ ! -d $sock ]; then
            echo "error: no such VDE interface ($QEMU_VDE)"
            return 1
        nohup vdeq $QEMU_BIN \
            -net vde,sock=$sock \
            -net nic,macaddr=$(qmacaddr $QEMU_MACADDR) \
            $QEMU_OPTIONS \
        nohup $QEMU_BIN $QEMU_OPTIONS $@


$ cat git
export GIT_AUTHOR_NAME="Liraz Siri"

git_branch_bin=$(which git-branch 2>/dev/null)
function git-branch()
    local delete=no
    for arg; do
        [ "$arg" == "-D" ] && delete=yes
    if [ "$delete" == "yes" ]; then
        $git_branch_bin -v $*
        $git_branch_bin -v -a $*

Oh and sorry for the late reply. I've been offline for most of these last two weeks. Too bad you didn't create an account because then you would get email notification on reply. I find that very useful myself in keeping track of the discussion (especially when it's late!).

curt smith's picture

I think Shell scripts allow several commands that would be entered manually at a command line interface to be executed automatically, and without having to wait for a user to trigger each stage of the sequence. For example, in a directory with three C source code files, rather than manually running the four commands required to build the final program from them, one could instead create a C shell script, here named build and kept in the directory with them, which would compile them automatically.



Link's picture

I am smacking my forehead now. Good work on this. Also, glad you defined "Scratch."

high availability's picture

I had trouble adding extra hooks while configuring Ssh session. I was trying to add extra stuff to alter the environment but they conflicted with some libraries and functions. I stumbled across your post recently and I am now trying to add generic hooks. So far it worked well. I changed the order and it so damn easy! Removing codes without the need to go through the whole of it and altering here and there. It is such a big blessing.

jetm's picture

run-parts util, it's installed by default in all majors distributions.

run-parts -a "$@" -- DIR

-a    pass all parameters to script
--     end for parameters
DIR directory with all hooks scripts

dav's picture

run-parts won't run the scripts in your current shell environment and so can't be used to alter that environment. 

kevin's picture

i know this thread is quite old, but i wanted to mention.. i took this a bit further and created ${HOME}/.rc.d. in there, i have FXX-name and AXX-name, F for functions, A for aliases. loads very nicely =)

Liraz Siri's picture

Hi Kevin, thanks for sharing. I never thought to separate shell hooks by function/alias. What's your rational for doing that?


Add new comment