Store a filename in a variable minus the extension












5















File: test.zip



Bash script



while read filename; do
zip_file=${filename}
# do stuff
done;


Value stored in variable = "test"










share|improve this question



























    5















    File: test.zip



    Bash script



    while read filename; do
    zip_file=${filename}
    # do stuff
    done;


    Value stored in variable = "test"










    share|improve this question

























      5












      5








      5


      1






      File: test.zip



      Bash script



      while read filename; do
      zip_file=${filename}
      # do stuff
      done;


      Value stored in variable = "test"










      share|improve this question














      File: test.zip



      Bash script



      while read filename; do
      zip_file=${filename}
      # do stuff
      done;


      Value stored in variable = "test"







      bash scripts






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 24 '15 at 14:00









      JohnnyBizzleJohnnyBizzle

      1702418




      1702418






















          3 Answers
          3






          active

          oldest

          votes


















          4














          Use bash parameter expansion:



          zip_file="${filename}"
          new_name="${zip_file%.*}"



          • new_name will contain the name test if the zip_file has test.zip



          • If the zip_file has test.foo.zip, new_name will have test.foo, if you want only test out of test.foo.zip use:



            new_name="${zip_file%%.*}"







          share|improve this answer


























          • This seems to include part of the path. I just want the file name. eg NOT ./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn

            – JohnnyBizzle
            May 24 '15 at 14:39













          • @JohnnyBizzle With due respect why would you give the filename as test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..

            – heemayl
            May 24 '15 at 14:42











          • I think you just need $(basename "${zip_file}") to get what I want.

            – JohnnyBizzle
            May 24 '15 at 15:34











          • @JohnnyBizzle no, you would need $(basename "${zipfile}" .zip) to get what you want.

            – muru
            May 24 '15 at 16:42



















          1














          heemayl is still right: example:



          full_path=/foo/bar/baz.zip
          file_name="${full_path##*/}"
          name="${file_name%.*}"





          share|improve this answer































            0














            Using sed:



            zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"




            • zip_file="$( [...] )": assigns the stdout of an invoked subshell to the variable zip_file as a string


            • <<< "${filename}" [...]: redirects the content of the variable ${filename} to the invoked subshell's stdin as a string


            • sed -r 's/^(.*)./1/': edits the content of the invoked subshell's stdin using extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot


            Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:



            zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"





            share|improve this answer

























              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "89"
              };
              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',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              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
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f627678%2fstore-a-filename-in-a-variable-minus-the-extension%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              4














              Use bash parameter expansion:



              zip_file="${filename}"
              new_name="${zip_file%.*}"



              • new_name will contain the name test if the zip_file has test.zip



              • If the zip_file has test.foo.zip, new_name will have test.foo, if you want only test out of test.foo.zip use:



                new_name="${zip_file%%.*}"







              share|improve this answer


























              • This seems to include part of the path. I just want the file name. eg NOT ./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn

                – JohnnyBizzle
                May 24 '15 at 14:39













              • @JohnnyBizzle With due respect why would you give the filename as test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..

                – heemayl
                May 24 '15 at 14:42











              • I think you just need $(basename "${zip_file}") to get what I want.

                – JohnnyBizzle
                May 24 '15 at 15:34











              • @JohnnyBizzle no, you would need $(basename "${zipfile}" .zip) to get what you want.

                – muru
                May 24 '15 at 16:42
















              4














              Use bash parameter expansion:



              zip_file="${filename}"
              new_name="${zip_file%.*}"



              • new_name will contain the name test if the zip_file has test.zip



              • If the zip_file has test.foo.zip, new_name will have test.foo, if you want only test out of test.foo.zip use:



                new_name="${zip_file%%.*}"







              share|improve this answer


























              • This seems to include part of the path. I just want the file name. eg NOT ./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn

                – JohnnyBizzle
                May 24 '15 at 14:39













              • @JohnnyBizzle With due respect why would you give the filename as test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..

                – heemayl
                May 24 '15 at 14:42











              • I think you just need $(basename "${zip_file}") to get what I want.

                – JohnnyBizzle
                May 24 '15 at 15:34











              • @JohnnyBizzle no, you would need $(basename "${zipfile}" .zip) to get what you want.

                – muru
                May 24 '15 at 16:42














              4












              4








              4







              Use bash parameter expansion:



              zip_file="${filename}"
              new_name="${zip_file%.*}"



              • new_name will contain the name test if the zip_file has test.zip



              • If the zip_file has test.foo.zip, new_name will have test.foo, if you want only test out of test.foo.zip use:



                new_name="${zip_file%%.*}"







              share|improve this answer















              Use bash parameter expansion:



              zip_file="${filename}"
              new_name="${zip_file%.*}"



              • new_name will contain the name test if the zip_file has test.zip



              • If the zip_file has test.foo.zip, new_name will have test.foo, if you want only test out of test.foo.zip use:



                new_name="${zip_file%%.*}"








              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jan 26 at 18:00

























              answered May 24 '15 at 14:05









              heemaylheemayl

              67.2k9142214




              67.2k9142214













              • This seems to include part of the path. I just want the file name. eg NOT ./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn

                – JohnnyBizzle
                May 24 '15 at 14:39













              • @JohnnyBizzle With due respect why would you give the filename as test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..

                – heemayl
                May 24 '15 at 14:42











              • I think you just need $(basename "${zip_file}") to get what I want.

                – JohnnyBizzle
                May 24 '15 at 15:34











              • @JohnnyBizzle no, you would need $(basename "${zipfile}" .zip) to get what you want.

                – muru
                May 24 '15 at 16:42



















              • This seems to include part of the path. I just want the file name. eg NOT ./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn

                – JohnnyBizzle
                May 24 '15 at 14:39













              • @JohnnyBizzle With due respect why would you give the filename as test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..

                – heemayl
                May 24 '15 at 14:42











              • I think you just need $(basename "${zip_file}") to get what I want.

                – JohnnyBizzle
                May 24 '15 at 15:34











              • @JohnnyBizzle no, you would need $(basename "${zipfile}" .zip) to get what you want.

                – muru
                May 24 '15 at 16:42

















              This seems to include part of the path. I just want the file name. eg NOT ./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn

              – JohnnyBizzle
              May 24 '15 at 14:39







              This seems to include part of the path. I just want the file name. eg NOT ./Somefolder/Somefolder/FilenameminusExtn but FilenameminusExtn

              – JohnnyBizzle
              May 24 '15 at 14:39















              @JohnnyBizzle With due respect why would you give the filename as test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..

              – heemayl
              May 24 '15 at 14:42





              @JohnnyBizzle With due respect why would you give the filename as test.zip, why don't you include the path or mention that there would be path before the file name? Please edit your question and add some examples (be precise) and your desired output (be precise)..

              – heemayl
              May 24 '15 at 14:42













              I think you just need $(basename "${zip_file}") to get what I want.

              – JohnnyBizzle
              May 24 '15 at 15:34





              I think you just need $(basename "${zip_file}") to get what I want.

              – JohnnyBizzle
              May 24 '15 at 15:34













              @JohnnyBizzle no, you would need $(basename "${zipfile}" .zip) to get what you want.

              – muru
              May 24 '15 at 16:42





              @JohnnyBizzle no, you would need $(basename "${zipfile}" .zip) to get what you want.

              – muru
              May 24 '15 at 16:42













              1














              heemayl is still right: example:



              full_path=/foo/bar/baz.zip
              file_name="${full_path##*/}"
              name="${file_name%.*}"





              share|improve this answer




























                1














                heemayl is still right: example:



                full_path=/foo/bar/baz.zip
                file_name="${full_path##*/}"
                name="${file_name%.*}"





                share|improve this answer


























                  1












                  1








                  1







                  heemayl is still right: example:



                  full_path=/foo/bar/baz.zip
                  file_name="${full_path##*/}"
                  name="${file_name%.*}"





                  share|improve this answer













                  heemayl is still right: example:



                  full_path=/foo/bar/baz.zip
                  file_name="${full_path##*/}"
                  name="${file_name%.*}"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 24 '15 at 15:00









                  StephenStephen

                  1,657711




                  1,657711























                      0














                      Using sed:



                      zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"




                      • zip_file="$( [...] )": assigns the stdout of an invoked subshell to the variable zip_file as a string


                      • <<< "${filename}" [...]: redirects the content of the variable ${filename} to the invoked subshell's stdin as a string


                      • sed -r 's/^(.*)./1/': edits the content of the invoked subshell's stdin using extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot


                      Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:



                      zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"





                      share|improve this answer






























                        0














                        Using sed:



                        zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"




                        • zip_file="$( [...] )": assigns the stdout of an invoked subshell to the variable zip_file as a string


                        • <<< "${filename}" [...]: redirects the content of the variable ${filename} to the invoked subshell's stdin as a string


                        • sed -r 's/^(.*)./1/': edits the content of the invoked subshell's stdin using extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot


                        Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:



                        zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"





                        share|improve this answer




























                          0












                          0








                          0







                          Using sed:



                          zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"




                          • zip_file="$( [...] )": assigns the stdout of an invoked subshell to the variable zip_file as a string


                          • <<< "${filename}" [...]: redirects the content of the variable ${filename} to the invoked subshell's stdin as a string


                          • sed -r 's/^(.*)./1/': edits the content of the invoked subshell's stdin using extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot


                          Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:



                          zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"





                          share|improve this answer















                          Using sed:



                          zip_file="$(<<< "${filename}" sed -r 's/^(.*)..*/1/')"




                          • zip_file="$( [...] )": assigns the stdout of an invoked subshell to the variable zip_file as a string


                          • <<< "${filename}" [...]: redirects the content of the variable ${filename} to the invoked subshell's stdin as a string


                          • sed -r 's/^(.*)./1/': edits the content of the invoked subshell's stdin using extended regular expressions, by matching the whole string and replacing it with the substring matching every character from the start until the last dot


                          Edit: Having seen your comment to heemayl's answer, to replace with the substring matching every character from the last slash to the last dot:



                          zip_file="$(<<< "${filename}" sed -r 's/^.*/(.*)..*/1/')"






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited May 24 '15 at 15:36

























                          answered May 24 '15 at 15:25









                          koskos

                          25.8k871121




                          25.8k871121






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Ask Ubuntu!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f627678%2fstore-a-filename-in-a-variable-minus-the-extension%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

                              Human spaceflight

                              Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                              File:DeusFollowingSea.jpg