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.










share|improve this question









New contributor




Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    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.










    share|improve this question









    New contributor




    Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      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.










      share|improve this question









      New contributor




      Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      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






      share|improve this question









      New contributor




      Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 days ago





















      New contributor




      Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Nov 22 at 17:00









      Will Da Silva

      1035




      1035




      New contributor




      Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Will Da Silva is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          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






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            Will Da Silva is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            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

























            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






            share|improve this answer



























              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






              share|improve this answer

























                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






                share|improve this answer














                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







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 22 at 18:53

























                answered Nov 22 at 18:42









                Tagwint

                1,4481714




                1,4481714






















                    Will Da Silva is a new contributor. Be nice, and check out our Code of Conduct.










                     

                    draft saved


                    draft discarded


















                    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.















                     


                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Accessing regular linux commands in Huawei's Dopra Linux

                    Can't connect RFCOMM socket: Host is down

                    Kernel panic - not syncing: Fatal Exception in Interrupt