Skip to content

Instantly share code, notes, and snippets.

@nivir
Forked from darrenderidder/bashpath.sh
Created January 1, 2021 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nivir/d2df3d649b0d7016e8fa937f195343db to your computer and use it in GitHub Desktop.
Save nivir/d2df3d649b0d7016e8fa937f195343db to your computer and use it in GitHub Desktop.
Get path of running script in bash
#!/bin/bash
# A simple test script to demonstrate how to find the
# "absolute path" at which a script is running. Used
# to avoid some of the pitfals of using 'pwd' or hard-
# coded paths when running scripts from cron or another
# directory.
#
# Try it out:
# run the script from the current directory, then
# cd.. and run it again (using the file path).
#
# You can see that CURDIR changes depending on where
# you run the script from, but ABSDIR stays the same.
CURDIR=`/bin/pwd`
BASEDIR=$(dirname $0)
ABSPATH=$(readlink -f $0)
ABSDIR=$(dirname $ABSPATH)
echo "CURDIR is $CURDIR"
echo "BASEDIR is $BASEDIR"
echo "ABSPATH is $ABSPATH"
echo "ABSDIR is $ABSDIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment