How to handle relative paths in a bash setup script?

Viewed 247

I often run into the situation where I would like to provide some kind of 'setup' or 'install' script, for something like a git repository, although it could also be used in other situations.

Just to be clear: with 'setup script' I mean a script which places some files, checks some things, creates certain dependencies and so on.

The problem is that if I want to use resources relative to the script or want to create links that target files in the repository I somehow need to be able to reference resources relative to the repository root or build absolute paths.

Currently I always go with this:

SCRIPT_DIR=$(cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd)
ROOT=$(realpath "$SCRIPT_DIR/..")

But this seems really bad, as it hard to understand and basically replicated in every repo or even file.

Is there a better way to do this? Are scripts like this unwanted?

3 Answers

Is there a better way to do this?

No.

Are scripts like this unwanted?

No. That's normal.

Is there another way to go about this?

My fingers are used to typing "$(dirname "$(readlink -f "$0")")", but that's not better.

Also do not use UPPER CASE VARIABLES. Not only they shout, but are meant for environment variables, like PWD LINES COLUMNS DISPLAY UID IFS USER etc. Prefer lower case variables in your scripts. (I would say that ROOT is a very common and thus bad variable name.)

In a git repo, you might use git rev-parse --show-toplevel to find the root of the worktree, and then go from there.

In general, it is a somewhat hard problem. There are too many ways to invoke a script that can alter what $0 actually means, so you can't really rely on it. In my opinion, the best you can do there is to establish a standard (for example, by expecting certain values in the environment or insisting the script be executed as ./path/from/root/to/script) and try to exit with good error messages if not.

Can this improve scripts management and readability ?

#!/usr/bin/env bash

source $HOME/common-tools/include.sh "${BASH_SOURCE[0]}"

echo $ROOT

in $HOME/common-tools/include.sh :

SCRIPT_DIR=$(cd "$( dirname "$1" )" >/dev/null 2>&1 && pwd)
ROOT=$(realpath "$SCRIPT_DIR/..")

Related