Canonical, Absolute and Relative Paths in POSIX Shell


/ Published in: Bash
Save to your folder(s)

Canonical, Absolute and Relative Paths in POSIX Shell


Copy this code and paste it in your HTML
  1. # Prepare
  2. mkdir -p "${HOME}/path without/symlinks"; ln -s "${HOME}/path without" "${HOME}/path with"
  3.  
  4. TESTPATH="${HOME}/..///${USER}/path with/symlinks///"; echo "${TESTPATH}"
  5.  
  6. echo "Absolute path: '$(realpath -m ${TESTPATH})'"
  7. echo "Canonical path: '$(realpath -s -m ${TESTPATH})'"
  8. echo "Relative to '/usr/bin': '$(realpath -s -m --relative-to="/usr/bin" ${TESTPATH})'"
  9. echo "Canonical relative to '/usr/bin': '$(realpath -m --relative-to="/usr/bin" ${TESTPATH})'"
  10. echo "Relative with base '/usr/bin': '$(realpath -s -m --relative-base="/usr/bin" ${TESTPATH})'"
  11. echo "Relative with base '${HOME}': '$(realpath -s -m --relative-base="${HOME}" ${TESTPATH})'"
  12. echo "Canonical relative with base '${HOME}': '$(realpath -m --relative-base="${HOME}" ${TESTPATH})'"
  13. echo "Basedir: '$(dirname "$(realpath -s -m ${TESTPATH})")'"
  14. # BASEDIR="$(dirname "$(realpath -s ${0})")"
  15.  
  16. # Cleanup
  17. rm -rf "${HOME}/path with" "${HOME}/path without"

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.