Ubuntu – How to get the directrory name in shell script

bashenvironment-variablesscripts

I have a script which sets some environment variable to paths:

export VAR1=/home/xyz/DIR_IN_WHICH_THIS_FILE_EXISTS

export VAR2=$VAR1/abc/qwe

...

Can I use something to get the DIR_IN_WHICH_THIS_FILE_EXISTS so that if I run this script from anywhere(any path) in the machine, it would set the same values to VAR1 depend on in which directory the script exists?

Please help

Best Answer

I think that you are interested about the path of the running script. If so, you should use dirname:

export VAR1=$(dirname "${BASH_SOURCE[0]}")

where ${BASH_SOURCE[0]} refers to the name of the curent running script.