bash / ${APP_HOME}

During the production and deployment of any application larger than “hello world”, careful thought must be given to accommodating a likely set of changes.

One of these is deployment location — the application must not have any hard-coded file system locations present, and must determine its location at runtime.

Every bash script should start with this little section.

readonly _REALPATH=$(realpath "${BASH_SOURCE[ 0 ]}")
readonly _BASENAME=$(basename "${_REALPATH}")
readonly _DIRNAME=$(dirname "${_REALPATH}")
readonly _ROOT=$(dirname "${_DIRNAME}")
readonly _PREFIX=$(dirname "${_ROOT}")

Then you can say, for example:

export THIS_APP_HOME=${_ROOT}

and use that variable within and across the whole application (call it “this-app”) and pass it in as an application property, for example. If the script above lives at /deploy/some/where/this-app/bin/start-component everything will be relative to /deploy/some/where and if you need to refer to another application (call it “other-app”) installed at the same level, you can say

export OTHER_APP_HOME=${_PREFIX}/other-app/ 

Remember that it’s helpful to use a prefix such as “THIS_APP” for environment variables for applications.

Leave a Reply

Your email address will not be published.