Created
March 18, 2015 09:25
-
-
Save hubertursua/c3d3f77d6f6fd7e088dd to your computer and use it in GitHub Desktop.
Get path of running script relative to present working directory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Hyubs Ursua <[email protected]> | |
# 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