Skip to content

Instantly share code, notes, and snippets.

@leodutra
Created February 3, 2016 13:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leodutra/795fa027e273fffe5b8b to your computer and use it in GitHub Desktop.
Save leodutra/795fa027e273fffe5b8b to your computer and use it in GitHub Desktop.
Getting absolute path of a shell / bash script file in execution time
#!/bin/bash
# REFERENCE: http://stackoverflow.com/a/1638397/1260526
# Absolute path to this script, e.g. /home/user/bin/foo.sh
SCRIPT=$(readlink -f "$0")
# Absolute path this script is in, thus /home/user/bin
SCRIPTPATH=$(dirname "$SCRIPT")
echo $SCRIPTPATH
@ORESoftware
Copy link

You can use the built-in $BASH_SOURCE env var too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment