Simple “Variable” Bash File












5















I just want to make a bash file that takes a file name as a parameter. In that bash file, I want to take the name before the extension and have it as a "variable", to use in other places. For example, if I was to run bash_script_name sample.asm, the bash script would run:



nasm -f elf sample.asm
ld -s -o sample sample.o io.o


So basically the form of bash_script_name $().asm



nasm -f elf $().asm
ld -s -o $() $().o io.o


... however I would do that in bash.










share|improve this question





























    5















    I just want to make a bash file that takes a file name as a parameter. In that bash file, I want to take the name before the extension and have it as a "variable", to use in other places. For example, if I was to run bash_script_name sample.asm, the bash script would run:



    nasm -f elf sample.asm
    ld -s -o sample sample.o io.o


    So basically the form of bash_script_name $().asm



    nasm -f elf $().asm
    ld -s -o $() $().o io.o


    ... however I would do that in bash.










    share|improve this question



























      5












      5








      5








      I just want to make a bash file that takes a file name as a parameter. In that bash file, I want to take the name before the extension and have it as a "variable", to use in other places. For example, if I was to run bash_script_name sample.asm, the bash script would run:



      nasm -f elf sample.asm
      ld -s -o sample sample.o io.o


      So basically the form of bash_script_name $().asm



      nasm -f elf $().asm
      ld -s -o $() $().o io.o


      ... however I would do that in bash.










      share|improve this question
















      I just want to make a bash file that takes a file name as a parameter. In that bash file, I want to take the name before the extension and have it as a "variable", to use in other places. For example, if I was to run bash_script_name sample.asm, the bash script would run:



      nasm -f elf sample.asm
      ld -s -o sample sample.o io.o


      So basically the form of bash_script_name $().asm



      nasm -f elf $().asm
      ld -s -o $() $().o io.o


      ... however I would do that in bash.







      bash shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 1 hour ago









      Rui F Ribeiro

      41.5k1483140




      41.5k1483140










      asked Nov 13 '10 at 21:57









      ChigginsChiggins

      1604




      1604






















          2 Answers
          2






          active

          oldest

          votes


















          10














          The first argument to the script will be in $1. You can use a bash string replacement to pull the extension; this removes everything from the last occurrence of . forward, and stores the result in $filename:



          filename="${1%.*}"


          Then you can use $filename in your script wherever you want:



          nasm -f elf "$filename.asm"
          ld -s -o "$filename" "$filename".o io.o





          share|improve this answer

































            0














            This particular answer to a similar question on Stack Overflow seems to have been very well thought out.
            It includes logic to deal with files with no dots in them ("somefile"), and files with multiple dots in them ("somefile.tar.gz").






            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',
              autoActivateHeartbeat: false,
              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
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f4041%2fsimple-variable-bash-file%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              10














              The first argument to the script will be in $1. You can use a bash string replacement to pull the extension; this removes everything from the last occurrence of . forward, and stores the result in $filename:



              filename="${1%.*}"


              Then you can use $filename in your script wherever you want:



              nasm -f elf "$filename.asm"
              ld -s -o "$filename" "$filename".o io.o





              share|improve this answer






























                10














                The first argument to the script will be in $1. You can use a bash string replacement to pull the extension; this removes everything from the last occurrence of . forward, and stores the result in $filename:



                filename="${1%.*}"


                Then you can use $filename in your script wherever you want:



                nasm -f elf "$filename.asm"
                ld -s -o "$filename" "$filename".o io.o





                share|improve this answer




























                  10












                  10








                  10







                  The first argument to the script will be in $1. You can use a bash string replacement to pull the extension; this removes everything from the last occurrence of . forward, and stores the result in $filename:



                  filename="${1%.*}"


                  Then you can use $filename in your script wherever you want:



                  nasm -f elf "$filename.asm"
                  ld -s -o "$filename" "$filename".o io.o





                  share|improve this answer















                  The first argument to the script will be in $1. You can use a bash string replacement to pull the extension; this removes everything from the last occurrence of . forward, and stores the result in $filename:



                  filename="${1%.*}"


                  Then you can use $filename in your script wherever you want:



                  nasm -f elf "$filename.asm"
                  ld -s -o "$filename" "$filename".o io.o






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 13 '10 at 23:25

























                  answered Nov 13 '10 at 22:13









                  Michael MrozekMichael Mrozek

                  61.8k29192212




                  61.8k29192212

























                      0














                      This particular answer to a similar question on Stack Overflow seems to have been very well thought out.
                      It includes logic to deal with files with no dots in them ("somefile"), and files with multiple dots in them ("somefile.tar.gz").






                      share|improve this answer






























                        0














                        This particular answer to a similar question on Stack Overflow seems to have been very well thought out.
                        It includes logic to deal with files with no dots in them ("somefile"), and files with multiple dots in them ("somefile.tar.gz").






                        share|improve this answer




























                          0












                          0








                          0







                          This particular answer to a similar question on Stack Overflow seems to have been very well thought out.
                          It includes logic to deal with files with no dots in them ("somefile"), and files with multiple dots in them ("somefile.tar.gz").






                          share|improve this answer















                          This particular answer to a similar question on Stack Overflow seems to have been very well thought out.
                          It includes logic to deal with files with no dots in them ("somefile"), and files with multiple dots in them ("somefile.tar.gz").







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited May 23 '17 at 11:33









                          Community

                          1




                          1










                          answered Jun 10 '12 at 15:25









                          killermistkillermist

                          954922




                          954922






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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%2funix.stackexchange.com%2fquestions%2f4041%2fsimple-variable-bash-file%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

                              サソリ

                              広島県道265号伴広島線

                              Accessing regular linux commands in Huawei's Dopra Linux