Pages

Monday, January 4, 2016

Capture current directory within a shell script

Home

There are various scenarios where you need to ensure that all supporting components of a shell script are written in the same directory where script has been placed.

In order to achieve that easy way it to initialize a variable at start of script with value as parent folder of the shell script. But hard-coding of it can be a pain area in later on stages.

So to make it dynamic below syntax can be utilized, this has been tested and used in various implementations worth a try.

#!/bin/bash
SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

Enjoy !!


Back To Top
Home

No comments:

Post a Comment