Warning! This post is outdated. Be careful.

bashir: Bash, Execute Here Please

4 Mar 2013


make executes from the directory that contains the Makefile. This makes it easy to call tools: just specify the tool's path relative to the Makefile.

file_b: file_a
    ./make_file_b.sh file_a

However, you cannot make a script with a nice command-line interface using make - that's what Bash scripts are for. But, Bash scripts are executed from your working directory, not the directory of the script. So, you can't use relative paths, which can cause a lot of grief when calling scripts from within scripts.

The long way

A way of being able to use relative paths when calling scripts, is by adding

cd -- "$(
    realpath -- "$(
        dirname -- "$(
            realpath -- "${BASH_SOURCE[0]}"
        )"
    )"
)"

to the top of every bash script, but that's quite tedious.

The bashir way

Enter Bashir; install it and then change your shebangs from

1
#!/bin/bash

to

1
#!/usr/bin/bashir

and now when you execute your bashir scripts, e.g,

$ ./scripts/some_script.sh

they'll execute from the directory they're saved in.

Get it now from GitHub