Skip to content

Instantly share code, notes, and snippets.

@alvis
Created May 12, 2020 04:38
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 alvis/d554cd43ad8bebc62b03602963edb88d to your computer and use it in GitHub Desktop.
Save alvis/d554cd43ad8bebc62b03602963edb88d to your computer and use it in GitHub Desktop.
A procedure for getting the resolved absolute path of current file and its containing directory
#!/bin/sh
CWD=`pwd -P`
TARGET_FILE=$0
cd `dirname $TARGET_FILE`
TARGET_FILE=`basename $TARGET_FILE`
# iterate down a (possible) chain of symlinks
while [ -L "$TARGET_FILE" ]
do
TARGET_FILE=`readlink $TARGET_FILE`
cd `dirname $TARGET_FILE`
TARGET_FILE=`basename $TARGET_FILE`
done
# compute the canonicalized name by finding the physical path
BASE=`pwd -P`
# cd back to the original working directory
cd $CWD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment