How to get the current path without last folder in a script?

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











up vote
0
down vote

favorite












I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:



/tmp/test/test2/test3/test.sh



path=$PWD
echo $path


Gives /tmp/test/test2/test3.
But I want to exclude the folder where the sh script is in.



So my desired output would be:
/tmp/test/test2










share|improve this question



























    up vote
    0
    down vote

    favorite












    I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:



    /tmp/test/test2/test3/test.sh



    path=$PWD
    echo $path


    Gives /tmp/test/test2/test3.
    But I want to exclude the folder where the sh script is in.



    So my desired output would be:
    /tmp/test/test2










    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:



      /tmp/test/test2/test3/test.sh



      path=$PWD
      echo $path


      Gives /tmp/test/test2/test3.
      But I want to exclude the folder where the sh script is in.



      So my desired output would be:
      /tmp/test/test2










      share|improve this question















      I want to run a script that saves the current path into a variable, excluding the folder itself where the script runs in. Example:



      /tmp/test/test2/test3/test.sh



      path=$PWD
      echo $path


      Gives /tmp/test/test2/test3.
      But I want to exclude the folder where the sh script is in.



      So my desired output would be:
      /tmp/test/test2







      bash ubuntu






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 15 at 9:00

























      asked Aug 15 at 8:50









      membersound

      1066




      1066




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          2
          down vote













          try the following code,



          dirname "$(pwd)"


          • dirname - strip suffix from file/dir path





          share|improve this answer






















          • leave the variable empty. I'm on ubuntu if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
            – membersound
            Aug 15 at 9:00


















          up vote
          1
          down vote













          You may use



          dir=$PWD%/* # or: dir=$( dirname "$PWD" )


          or



          dir="$PWD/.."


          or



          dir=".."


          In the first case, the PWD variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last / and everything after it.



          In the second case, the PWD variable is used to simply create a path for the directory above using ...



          In the third case, we simply refer to the directory above using a relative path from the current directory.



          Which alternative you use is dependent on what you are going to use the path for.



          Examples:



          $ echo $PWD
          /tmp/shell-sh.jAtMYgn0




          $ dir=$PWD%/*
          $ echo "$dir"
          /tmp




          $ dir="$PWD/.."
          $ echo "$dir"
          /tmp/shell-sh.jAtMYgn0/..




          $ dir=".."
          $ echo "$dir"
          ..





          share|improve this answer



























            up vote
            -1
            down vote













            Finally it was as simple as: current=$(dirname "$(pwd)")






            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: 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%2f462701%2fhow-to-get-the-current-path-without-last-folder-in-a-script%23new-answer', 'question_page');

              );

              Post as a guest






























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              try the following code,



              dirname "$(pwd)"


              • dirname - strip suffix from file/dir path





              share|improve this answer






















              • leave the variable empty. I'm on ubuntu if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
                – membersound
                Aug 15 at 9:00















              up vote
              2
              down vote













              try the following code,



              dirname "$(pwd)"


              • dirname - strip suffix from file/dir path





              share|improve this answer






















              • leave the variable empty. I'm on ubuntu if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
                – membersound
                Aug 15 at 9:00













              up vote
              2
              down vote










              up vote
              2
              down vote









              try the following code,



              dirname "$(pwd)"


              • dirname - strip suffix from file/dir path





              share|improve this answer














              try the following code,



              dirname "$(pwd)"


              • dirname - strip suffix from file/dir path






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Aug 15 at 9:01

























              answered Aug 15 at 8:56









              msp9011

              3,46643862




              3,46643862











              • leave the variable empty. I'm on ubuntu if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
                – membersound
                Aug 15 at 9:00

















              • leave the variable empty. I'm on ubuntu if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
                – membersound
                Aug 15 at 9:00
















              leave the variable empty. I'm on ubuntu if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
              – membersound
              Aug 15 at 9:00





              leave the variable empty. I'm on ubuntu if that matters.. By the ways this works inside a terminal, but not in a script assigning it to a variable.
              – membersound
              Aug 15 at 9:00













              up vote
              1
              down vote













              You may use



              dir=$PWD%/* # or: dir=$( dirname "$PWD" )


              or



              dir="$PWD/.."


              or



              dir=".."


              In the first case, the PWD variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last / and everything after it.



              In the second case, the PWD variable is used to simply create a path for the directory above using ...



              In the third case, we simply refer to the directory above using a relative path from the current directory.



              Which alternative you use is dependent on what you are going to use the path for.



              Examples:



              $ echo $PWD
              /tmp/shell-sh.jAtMYgn0




              $ dir=$PWD%/*
              $ echo "$dir"
              /tmp




              $ dir="$PWD/.."
              $ echo "$dir"
              /tmp/shell-sh.jAtMYgn0/..




              $ dir=".."
              $ echo "$dir"
              ..





              share|improve this answer
























                up vote
                1
                down vote













                You may use



                dir=$PWD%/* # or: dir=$( dirname "$PWD" )


                or



                dir="$PWD/.."


                or



                dir=".."


                In the first case, the PWD variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last / and everything after it.



                In the second case, the PWD variable is used to simply create a path for the directory above using ...



                In the third case, we simply refer to the directory above using a relative path from the current directory.



                Which alternative you use is dependent on what you are going to use the path for.



                Examples:



                $ echo $PWD
                /tmp/shell-sh.jAtMYgn0




                $ dir=$PWD%/*
                $ echo "$dir"
                /tmp




                $ dir="$PWD/.."
                $ echo "$dir"
                /tmp/shell-sh.jAtMYgn0/..




                $ dir=".."
                $ echo "$dir"
                ..





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You may use



                  dir=$PWD%/* # or: dir=$( dirname "$PWD" )


                  or



                  dir="$PWD/.."


                  or



                  dir=".."


                  In the first case, the PWD variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last / and everything after it.



                  In the second case, the PWD variable is used to simply create a path for the directory above using ...



                  In the third case, we simply refer to the directory above using a relative path from the current directory.



                  Which alternative you use is dependent on what you are going to use the path for.



                  Examples:



                  $ echo $PWD
                  /tmp/shell-sh.jAtMYgn0




                  $ dir=$PWD%/*
                  $ echo "$dir"
                  /tmp




                  $ dir="$PWD/.."
                  $ echo "$dir"
                  /tmp/shell-sh.jAtMYgn0/..




                  $ dir=".."
                  $ echo "$dir"
                  ..





                  share|improve this answer












                  You may use



                  dir=$PWD%/* # or: dir=$( dirname "$PWD" )


                  or



                  dir="$PWD/.."


                  or



                  dir=".."


                  In the first case, the PWD variable, which contains the current working directory, is used to get the path of the directory one level up by removing the last / and everything after it.



                  In the second case, the PWD variable is used to simply create a path for the directory above using ...



                  In the third case, we simply refer to the directory above using a relative path from the current directory.



                  Which alternative you use is dependent on what you are going to use the path for.



                  Examples:



                  $ echo $PWD
                  /tmp/shell-sh.jAtMYgn0




                  $ dir=$PWD%/*
                  $ echo "$dir"
                  /tmp




                  $ dir="$PWD/.."
                  $ echo "$dir"
                  /tmp/shell-sh.jAtMYgn0/..




                  $ dir=".."
                  $ echo "$dir"
                  ..






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 15 at 9:04









                  Kusalananda

                  106k14209327




                  106k14209327




















                      up vote
                      -1
                      down vote













                      Finally it was as simple as: current=$(dirname "$(pwd)")






                      share|improve this answer
























                        up vote
                        -1
                        down vote













                        Finally it was as simple as: current=$(dirname "$(pwd)")






                        share|improve this answer






















                          up vote
                          -1
                          down vote










                          up vote
                          -1
                          down vote









                          Finally it was as simple as: current=$(dirname "$(pwd)")






                          share|improve this answer












                          Finally it was as simple as: current=$(dirname "$(pwd)")







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Aug 15 at 9:11









                          membersound

                          1066




                          1066



























                               

                              draft saved


                              draft discarded















































                               


                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f462701%2fhow-to-get-the-current-path-without-last-folder-in-a-script%23new-answer', 'question_page');

                              );

                              Post as a guest













































































                              Popular posts from this blog

                              How to check contact read email or not when send email to Individual?

                              Bahrain

                              Postfix configuration issue with fips on centos 7; mailgun relay