Logical and between directories in shell script Logical and between directories in shell script unix unix

Logical and between directories in shell script


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"


DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"|     |  |  |   |          |___ First value   |   |______ prints current working directory|     |  |  |   |               of array      |___Logical AND operator|     |  |  |   |__ Command to strip non-directory suffix|     |  |  ||     |  |  |__ Doing command substitution again to evaluate whats inside $()|     |  ||     |  |___ Changing directory|     ||     |_____ $() is construct for command substitution||____ Creating and assigning a variable called DIR