> For the complete documentation index, see [llms.txt](https://notes.eliasnorrby.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://notes.eliasnorrby.com/bash/directory-of-script.md).

# Directory of script

It's often useful to know the path of the script being executed, e.g. for sourcing other files.

```bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
```

This solution works as long as the last component of the path used to find the script is not a symlink.

Source: [stackoverflow](https://stackoverflow.com/a/246128)
