Skip to content

Instantly share code, notes, and snippets.

@hubertursua
Created March 18, 2015 09:25
Show Gist options
  • Save hubertursua/c3d3f77d6f6fd7e088dd to your computer and use it in GitHub Desktop.
Save hubertursua/c3d3f77d6f6fd7e088dd to your computer and use it in GitHub Desktop.
Get path of running script relative to present working directory
#!/bin/bash
# Hyubs Ursua <hyubs.u@gmail.com>
# MIT License
# Pre-requisite: If running OSX, install coreutils using homebrew or macports.
# The two if-statements checks and uses the correct command
if ! type "greadlink" > /dev/null 2>&1
then
CMD_READLINK=readlink
else
CMD_READLINK=greadlink
fi
if ! type "grealpath" > /dev/null 2>&1
then
CMD_REALPATH=realpath
else
CMD_REALPATH=grealpath
fi
# Get absolute path of running script
SCRIPT=$($CMD_READLINK -f "$0")
SCRIPT_PATH=$(dirname "$SCRIPT")
# Get path of running script relative to present working directory
PWD=`pwd`
REL_PATH=$($CMD_REALPATH --relative-to=$PWD $SCRIPT_PATH)
echo $REL_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment