/ Published in: Bash

Canonical, Absolute and Relative Paths in POSIX Shell
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
# Prepare mkdir -p "${HOME}/path without/symlinks"; ln -s "${HOME}/path without" "${HOME}/path with" TESTPATH="${HOME}/..///${USER}/path with/symlinks///"; echo "${TESTPATH}" echo "Absolute path: '$(realpath -m ${TESTPATH})'" echo "Canonical path: '$(realpath -s -m ${TESTPATH})'" echo "Relative to '/usr/bin': '$(realpath -s -m --relative-to="/usr/bin" ${TESTPATH})'" echo "Canonical relative to '/usr/bin': '$(realpath -m --relative-to="/usr/bin" ${TESTPATH})'" echo "Relative with base '/usr/bin': '$(realpath -s -m --relative-base="/usr/bin" ${TESTPATH})'" echo "Relative with base '${HOME}': '$(realpath -s -m --relative-base="${HOME}" ${TESTPATH})'" echo "Canonical relative with base '${HOME}': '$(realpath -m --relative-base="${HOME}" ${TESTPATH})'" echo "Basedir: '$(dirname "$(realpath -s -m ${TESTPATH})")'" # BASEDIR="$(dirname "$(realpath -s ${0})")" # Cleanup rm -rf "${HOME}/path with" "${HOME}/path without"
Comments
