Artificial intelligent assistant

Resolve relative path without resolving symbolic links in bash I'm looking for a portable way to resolve relative paths into absolute paths while not resolving any symbolic links. For example, run the following file `/home/nat/cat/bat/hat.sh` in bash: set -x pwd # cat is a symlink readlink -e .. # abs is an imaginary function that returns the path in the form specified by this question abs .. abs /home/./nat/../nat/cat Output: + pwd /home/nat/cat/bat + readlink -e .. /home/nat/cat-1.12.0 + abs .. /home/nat/cat + abs /home/./nat/../nat/cat /home/nat/cat Unfortunately I cannot use `realpath -s` for this, as it is not available by default on OSX.

This is how your imaginary function may look materialized:


abs () {
local _PWD _BN
[ -d "${1}" ] && _PWD="${1}"
[ -f "${1}" ] && { _PWD=$(dirname "${1}") ; _BN=/$(basename "${1}") ;}
pushd $_PWD >/dev/null
echo $(pwd)${_BN}
popd >/dev/null
}


You can pass either file or directory path as parameter. it then will go to the specified path and print out working directory then that is what you want. You may want to add some validations around against empty parameter, non-existing path or insufficient permissions etc. according to your specific needs - I ommitted that from this example.

the `pushd/popd` pair performs the jump to the target and back

`>/dev/null` prevents these commands from printing out directory stack what they by default do and that would spoil the desired output

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 4bd513f84a942e40514648a7829a1340