Skip to content

Bundling shell scripts

probonopd edited this page Apr 20, 2013 · 1 revision

Shell scripts occasionally hardcode paths to other files. This will break if these other files are relocated. Hence, you should refer to outside files always relative to the position of the shell script itself. This can be achieved with the following code:

#!/bin/sh
HERE=$(dirname $(readlink -f "${0}"))
echo "${HERE}"

This will always return the absolute path to the directory the script is located in, regardless of how it was called.

Assume you have a script /usr/bin/foo which wants to call /usr/bin/bar.

To make it relocateable, you need to change /usr/bin/foo so that instead of calling

/usr/bin/bar --dosomething

it contains

"${HERE}/bar" --dosomething