Posted By

biiont on 09/09/14


Tagged

Shell POSIX dash


Versions (?)

Canonical, Absolute and Relative Paths in POSIX Shell


 / Published in: Bash
 

Canonical, Absolute and Relative Paths in POSIX Shell

  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  

You need to login to post a comment.