Skip to content

Instantly share code, notes, and snippets.

@AndersDJohnson
Last active June 5, 2019 13:33
Show Gist options
  • Save AndersDJohnson/4243371 to your computer and use it in GitHub Desktop.
Save AndersDJohnson/4243371 to your computer and use it in GitHub Desktop.
bash script BASEDIR
# from: https://github.com/zimbatm/direnv/blob/3bb35d375331fca89614f0015b2a6cd21688ab79/bin/direnv
# usage: abs_dirname $filename
# finds the original $filename path and prints it's absolute folder path
abs_dirname() {
prev_path="$1"
# Resolve the symlink(s) recursively
while true; do
abs_path=`readlink "$prev_path"`
if [ -z "$abs_path" ]; then
abs_path="$prev_path"
break
else
prev_path="$abs_path"
fi
done
unset prev_path
# Get the absolute directory of the final $abs_path
orig_dir=`pwd`
cd `dirname "$abs_path"`
# prints an absolute path here
pwd
cd "$orig_dir"
unset abs_path orig_dir
}
#!/bin/bash
CURDIR=${PWD}
BASEDIR="$( cd "$( dirname "$(realpath $BASH_SOURCE)" )" && pwd )"
# This didn't support symlinked scripts
# BASEDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
#!/bin/bash
BASEDIR=$(dirname "$(readlink -f $0)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment