Shell script current directory? Shell script current directory? linux linux

Shell script current directory?


As already mentioned, the location will be where the script was called from. If you wish to have the script reference it's installed location, it's quite simple. Below is a snippet that will print the PWD and the installed directory:

#!/bin/bashecho "Script executed from: ${PWD}"BASEDIR=$(dirname $0)echo "Script location: ${BASEDIR}"

You're weclome


Most answers get you the current path and are context sensitive. In order to run your script from any directory, use the below snippet.

DIR="$( cd "$( dirname "$0" )" && pwd )"

By switching directories in a subshell, we can then call pwd and get the correct path of the script regardless of context.

You can then use $DIR as "$DIR/path/to/file"


The current(initial) directory of shell script is the directory from which you have called the script.