I'm updating an old shell script to run with a new configuration and I'm relatively new to shell scripting, but I've been generally alright for most of the script. However, I'm having trouble figuring out exactly what the following line is doing. This particular line is being called from another running script and is being run on UNIX type machines, though I'm not sure how relevant that is.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
My question is basically, what directory is this actually pointing to relative to the directory it is called in and stored in? Also what exactly is that && doing there? It seems very odd to see a logical operator beween two directories, but again I'm fairly new to shell scripts.
It just saves the dir the script is located in:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
The commandA && commandB
condition is evaluated like this:
commandB
is executed if, and only if, commandA
returns an exit status of zero. Being cd something
, it will return true if the dir something
exists. If not, it will return an exit status false so pwd
will not be executed.
Graphically, it can be explained as:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
DIR="$( cd ( dir ( name_of_script ) ) && print current dir )"
DIR="$( move to the dir of the script && print current dir )"
DIR= "name of the dir you have moved , that is, the dir of the script"