Getting the working directory

Files / Basic operations

#$PWD keeps the working directory, the command pwd prints it
path1=$PWD
path2=$(pwd)
[[ "$path1" == "$path2" ]] && same=true || same=false
#same is true
#path1 is a temporary directory in the sandbox,
#on your machine the directory the script started from

#the directory of the script itself, wherever it was started from
scriptDir=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
[[ "$scriptDir" == "$path1" ]] && sameAsScript=true || sameAsScript=false
#sameAsScript is true: the sandbox starts main.sh in its directory

#relative paths like "file.txt" start from this directory;
#cd changes it for the rest of the script, OLDPWD keeps the previous one
cd /
#PWD is "/"
echo "PWD is $PWD"

#cd - goes back, but prints the directory: > /dev/null hides it
cd - > /dev/null
[[ "$PWD" == "$path1" ]] && back=true || back=false
#back is true

echo "same is $same"
echo "sameAsScript is $sameAsScript"
echo "back is $back"