Resolve relative path without resolving symbolic links in bash
up vote
0
down vote
favorite
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.
bash shell-script osx
New contributor
add a comment |
up vote
0
down vote
favorite
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.
bash shell-script osx
New contributor
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
bash shell-script osx
New contributor
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.
bash shell-script osx
bash shell-script osx
New contributor
New contributor
edited 2 days ago
New contributor
asked Nov 22 at 17:00
Will Da Silva
1035
1035
New contributor
New contributor
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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
add a comment |
up vote
2
down vote
accepted
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
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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
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
edited Nov 22 at 18:53
answered Nov 22 at 18:42
Tagwint
1,4481714
1,4481714
add a comment |
add a comment |
Will Da Silva is a new contributor. Be nice, and check out our Code of Conduct.
Will Da Silva is a new contributor. Be nice, and check out our Code of Conduct.
Will Da Silva is a new contributor. Be nice, and check out our Code of Conduct.
Will Da Silva is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f483492%2fresolve-relative-path-without-resolving-symbolic-links-in-bash%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown