How to store the full-path of a directory from a bash script?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I want to get the full-path director from a script, even if the user enters the './' or '/~'



#!/bin/bash
echo "Enter the directory"
read DIRECTORY #how to store the full-path instead of something like ./
echo "$DIRECTORY"









share|improve this question



























    up vote
    2
    down vote

    favorite












    I want to get the full-path director from a script, even if the user enters the './' or '/~'



    #!/bin/bash
    echo "Enter the directory"
    read DIRECTORY #how to store the full-path instead of something like ./
    echo "$DIRECTORY"









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I want to get the full-path director from a script, even if the user enters the './' or '/~'



      #!/bin/bash
      echo "Enter the directory"
      read DIRECTORY #how to store the full-path instead of something like ./
      echo "$DIRECTORY"









      share|improve this question















      I want to get the full-path director from a script, even if the user enters the './' or '/~'



      #!/bin/bash
      echo "Enter the directory"
      read DIRECTORY #how to store the full-path instead of something like ./
      echo "$DIRECTORY"






      linux bash scripting






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 4 hours ago

























      asked 4 hours ago









      Bionix1441

      17111




      17111




















          5 Answers
          5






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Use realpath:



          echo "$(realpath $DIRECTORY)"





          share|improve this answer
















          • 1




            This removes symbolic links, which is usually not desirable. Also you left out required double quotes and -- so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/…
            – Gilles
            3 hours ago

















          up vote
          2
          down vote













          For a directory, you can use the command pwd or the variable $PWD.



          Example



          $ DIRECTORY=.
          $ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
          $ echo $TRUEDIR
          /home/steve
          $





          share|improve this answer





























            up vote
            1
            down vote













            An absolute path begins with /, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.



            case $DIRECTORY in
            /*) DIRECTORY="$PWD/$DIRECTORY";;
            esac


            This is portable code: it works in plain sh, not just in bash.



            The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link






            share|improve this answer



























              up vote
              0
              down vote













              You may add a test for an absolute path:



              read DIRECTORY
              [ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
              echo "$DIRECTORY"





              share|improve this answer



























                up vote
                0
                down vote













                realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:



                readlink -f "$DIRECTORY"



                It too handles ~ and ./ and will follow symbolic links.






                share|improve this answer








                New contributor




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

















                  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: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475030%2fhow-to-store-the-full-path-of-a-directory-from-a-bash-script%23new-answer', 'question_page');

                  );

                  Post as a guest






























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  0
                  down vote



                  accepted










                  Use realpath:



                  echo "$(realpath $DIRECTORY)"





                  share|improve this answer
















                  • 1




                    This removes symbolic links, which is usually not desirable. Also you left out required double quotes and -- so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/…
                    – Gilles
                    3 hours ago














                  up vote
                  0
                  down vote



                  accepted










                  Use realpath:



                  echo "$(realpath $DIRECTORY)"





                  share|improve this answer
















                  • 1




                    This removes symbolic links, which is usually not desirable. Also you left out required double quotes and -- so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/…
                    – Gilles
                    3 hours ago












                  up vote
                  0
                  down vote



                  accepted







                  up vote
                  0
                  down vote



                  accepted






                  Use realpath:



                  echo "$(realpath $DIRECTORY)"





                  share|improve this answer












                  Use realpath:



                  echo "$(realpath $DIRECTORY)"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 4 hours ago









                  Ipor Sircer

                  9,5011920




                  9,5011920







                  • 1




                    This removes symbolic links, which is usually not desirable. Also you left out required double quotes and -- so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/…
                    – Gilles
                    3 hours ago












                  • 1




                    This removes symbolic links, which is usually not desirable. Also you left out required double quotes and -- so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/…
                    – Gilles
                    3 hours ago







                  1




                  1




                  This removes symbolic links, which is usually not desirable. Also you left out required double quotes and -- so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/…
                  – Gilles
                  3 hours ago




                  This removes symbolic links, which is usually not desirable. Also you left out required double quotes and -- so your code doesn't work with file names containing whitespace, among others. See unix.stackexchange.com/questions/131766/…
                  – Gilles
                  3 hours ago












                  up vote
                  2
                  down vote













                  For a directory, you can use the command pwd or the variable $PWD.



                  Example



                  $ DIRECTORY=.
                  $ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
                  $ echo $TRUEDIR
                  /home/steve
                  $





                  share|improve this answer


























                    up vote
                    2
                    down vote













                    For a directory, you can use the command pwd or the variable $PWD.



                    Example



                    $ DIRECTORY=.
                    $ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
                    $ echo $TRUEDIR
                    /home/steve
                    $





                    share|improve this answer
























                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      For a directory, you can use the command pwd or the variable $PWD.



                      Example



                      $ DIRECTORY=.
                      $ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
                      $ echo $TRUEDIR
                      /home/steve
                      $





                      share|improve this answer














                      For a directory, you can use the command pwd or the variable $PWD.



                      Example



                      $ DIRECTORY=.
                      $ TRUEDIR=$(cd -- "$DIRECTORY" && pwd)
                      $ echo $TRUEDIR
                      /home/steve
                      $






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited 2 hours ago

























                      answered 4 hours ago









                      rusty shackleford

                      1,275116




                      1,275116




















                          up vote
                          1
                          down vote













                          An absolute path begins with /, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.



                          case $DIRECTORY in
                          /*) DIRECTORY="$PWD/$DIRECTORY";;
                          esac


                          This is portable code: it works in plain sh, not just in bash.



                          The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link






                          share|improve this answer
























                            up vote
                            1
                            down vote













                            An absolute path begins with /, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.



                            case $DIRECTORY in
                            /*) DIRECTORY="$PWD/$DIRECTORY";;
                            esac


                            This is portable code: it works in plain sh, not just in bash.



                            The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link






                            share|improve this answer






















                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









                              An absolute path begins with /, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.



                              case $DIRECTORY in
                              /*) DIRECTORY="$PWD/$DIRECTORY";;
                              esac


                              This is portable code: it works in plain sh, not just in bash.



                              The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link






                              share|improve this answer












                              An absolute path begins with /, a relative one doesn't. You can turn a relative path into an absolute path by adding the path to the current directory and a slash before the relative path.



                              case $DIRECTORY in
                              /*) DIRECTORY="$PWD/$DIRECTORY";;
                              esac


                              This is portable code: it works in plain sh, not just in bash.



                              The resulting path may contain symbolic links. This is almost always the right thing, but if you want to canonicalize symbolic links, see Converting relative path to absolute path without symbolic link







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered 3 hours ago









                              Gilles

                              514k12110191549




                              514k12110191549




















                                  up vote
                                  0
                                  down vote













                                  You may add a test for an absolute path:



                                  read DIRECTORY
                                  [ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
                                  echo "$DIRECTORY"





                                  share|improve this answer
























                                    up vote
                                    0
                                    down vote













                                    You may add a test for an absolute path:



                                    read DIRECTORY
                                    [ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
                                    echo "$DIRECTORY"





                                    share|improve this answer






















                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      You may add a test for an absolute path:



                                      read DIRECTORY
                                      [ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
                                      echo "$DIRECTORY"





                                      share|improve this answer












                                      You may add a test for an absolute path:



                                      read DIRECTORY
                                      [ "$DIRECTORY:0:1" = "/" ] || DIRECTORY="$PWD/$DIRECTORY"
                                      echo "$DIRECTORY"






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered 4 hours ago









                                      RudiC

                                      1,91219




                                      1,91219




















                                          up vote
                                          0
                                          down vote













                                          realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:



                                          readlink -f "$DIRECTORY"



                                          It too handles ~ and ./ and will follow symbolic links.






                                          share|improve this answer








                                          New contributor




                                          Glenn 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













                                            realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:



                                            readlink -f "$DIRECTORY"



                                            It too handles ~ and ./ and will follow symbolic links.






                                            share|improve this answer








                                            New contributor




                                            Glenn 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










                                              up vote
                                              0
                                              down vote









                                              realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:



                                              readlink -f "$DIRECTORY"



                                              It too handles ~ and ./ and will follow symbolic links.






                                              share|improve this answer








                                              New contributor




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









                                              realpath (the accepted answer) is deprecated and not available on most installs. I would use its replacement:



                                              readlink -f "$DIRECTORY"



                                              It too handles ~ and ./ and will follow symbolic links.







                                              share|improve this answer








                                              New contributor




                                              Glenn 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 answer



                                              share|improve this answer






                                              New contributor




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









                                              answered 24 mins ago









                                              Glenn

                                              1




                                              1




                                              New contributor




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





                                              New contributor





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






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



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f475030%2fhow-to-store-the-full-path-of-a-directory-from-a-bash-script%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest













































































                                                  Comments

                                                  Popular posts from this blog

                                                  What does second last employer means? [closed]

                                                  Installing NextGIS Connect into QGIS 3?

                                                  One-line joke