Skip to content

Instantly share code, notes, and snippets.

@eeichinger
Created April 19, 2017 17:37
Show Gist options
  • Save eeichinger/0f744d961239a029295f145b9398d0af to your computer and use it in GitHub Desktop.
Save eeichinger/0f744d961239a029295f145b9398d0af to your computer and use it in GitHub Desktop.
Various ways to resolve a script path in bash
#!/usr/bin/env bash
#
# For my own reference in the future demo's various ways to resolve a script's full qualified path
# Usage: copy this file to /tmp/resolve_paths_demo.sh and run it from root
#
# Note: this script uses the 'realpath' utility - to get this on OSX, you need to install 'brew install coreutils'
if [ -z "${doit+x}" ]; then # for if [exists $var] technique, see http://stackoverflow.com/a/13864829/51264
scriptfilereal=$(realpath -P "$0")
rm -rf /tmp/resolve_path_test >/dev/null
mkdir -p /tmp/resolve_path_test/real/
mkdir -p /tmp/resolve_path_test/other_real/
mkdir -p /tmp/resolve_path_test/curdir/
cat > /tmp/resolve_path_test/real/demo_resolve_paths.sh <<EOF
#!/usr/bin/env bash
doit=1
EOF
chmod +x /tmp/resolve_path_test/real/demo_resolve_paths.sh
cat $scriptfilereal >> /tmp/resolve_path_test/real/demo_resolve_paths.sh
# symlink parent folder
ln -s /tmp/resolve_path_test/real /tmp/resolve_path_test/linked_real
# symlink file itself
ln -s /tmp/resolve_path_test/real/demo_resolve_paths.sh /tmp/resolve_path_test/other_real/linked_demo_resolve_paths.sh
pushd /tmp/resolve_path_test/curdir/ >/dev/null
echo ""
echo "# calling real path, real file: ../real/demo_resolve_paths.sh"
../real/demo_resolve_paths.sh
echo ""
echo "# calling symlinked path, real file: ../linked_real/demo_resolve_paths.sh"
../linked_real/demo_resolve_paths.sh
echo ""
echo "# calling other_real path, symlinked file: ../other_real/linked_demo_resolve_paths.sh"
../other_real/linked_demo_resolve_paths.sh
echo ""
popd >/dev/null
rm -rf /tmp/resolve_path_test >/dev/null
exit 0;
fi
scriptcmd=$0
scriptcmddir=${0%/*} # alternative to dirname, see http://stackoverflow.com/a/192524/51264
scriptfile=$(realpath -s "$0")
scriptpath=${scriptfile%/*}
scriptfilereal=$(realpath -P "$0")
echo "Script (cmd): $scriptcmd"
echo "Script (cmd path): $scriptcmddir"
echo "Script (absolute path): $scriptpath"
echo "Script (cmd absolute): $scriptfile"
echo "Script (cmd symlinks resolved): $scriptfilereal"
# calling real path, real file: ../real/demo_resolve_paths.sh
Script (cmd): ../real/demo_resolve_paths.sh
Script (cmd path): ../real
Script (absolute path): /private/tmp/resolve_path_test/real
Script (cmd absolute): /private/tmp/resolve_path_test/real/demo_resolve_paths.sh
Script (cmd symlinks resolved): /private/tmp/resolve_path_test/real/demo_resolve_paths.sh
# calling symlinked path, real file: ../linked_real/demo_resolve_paths.sh
Script (cmd): ../linked_real/demo_resolve_paths.sh
Script (cmd path): ../linked_real
Script (absolute path): /private/tmp/resolve_path_test/linked_real
Script (cmd absolute): /private/tmp/resolve_path_test/linked_real/demo_resolve_paths.sh
Script (cmd symlinks resolved): /private/tmp/resolve_path_test/real/demo_resolve_paths.sh
# calling other_real path, symlinked file: ../other_real/linked_demo_resolve_paths.sh
Script (cmd): ../other_real/linked_demo_resolve_paths.sh
Script (cmd path): ../other_real
Script (absolute path): /private/tmp/resolve_path_test/other_real
Script (cmd absolute): /private/tmp/resolve_path_test/other_real/linked_demo_resolve_paths.sh
Script (cmd symlinks resolved): /private/tmp/resolve_path_test/real/demo_resolve_paths.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment