0

This question already has an answer here:

There is my simple script that I try

#!/bin/bash DIR="$(cd "$(dirname $0)" && pwd)" echo $DIR

When I execute it like $ ./my_script.sh, get the path correctly.
But, when I source it like $ source my_script.sh, it gets the path /bin

How can I get the path of script by source it?
What is the different between source and execute?

marked as duplicate by Kusalananda bash Mar 19 at 11:26

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

The difference between sourcing a a script and "running it" is that when you source it (using source or .), the script is executed in the current shell environment, while if you "run it", a new shell process is started. You want to source scripts that you want would change the current shell environment. A script that is run in a separate shell process can not change the parent shell's environment. By "environment" is meant, for example, values of shell and environment variables, the current working directory etc.

A script is usually written to either be sourced or to be executed in its own shell environment, but very seldom both. A script that is made to be sourced is sometimes called a "dot-script" (since . is the standard command for sourcing such a script; source is a bash "alias" for .).

A dot-script being sourced by a bash shell can find its location by examining the first element of the BASH_SOURCE array:

printf 'My location: %s\n' "$( dirname "${BASH_SOURCE[0]}" )"

The directory path will be relative to the current working directory at the time of invoking source or . on the script.

Not the answer you're looking for? Browse other questions tagged or ask your own question.