How to create alias that takes a positional argument in Linux bash?












2














Lets say I have a bash command with a couple of options and the variable that I am interested in (e.g. filename):



my_cmd option1 option2 filename


I created an alias:



alias my_cmd_12="my_cmd option1 option2"


this allows me to remove typing all of the options. However there are some flags coming after the variable I am interested (e.g. filename):



my_cmd option1 option2 filename --flag1


How do I create an alias that takes all option an flags:



my_alias filename is equivalent to



my_cmd option1 option2 filename --flag1










share|improve this question



























    2














    Lets say I have a bash command with a couple of options and the variable that I am interested in (e.g. filename):



    my_cmd option1 option2 filename


    I created an alias:



    alias my_cmd_12="my_cmd option1 option2"


    this allows me to remove typing all of the options. However there are some flags coming after the variable I am interested (e.g. filename):



    my_cmd option1 option2 filename --flag1


    How do I create an alias that takes all option an flags:



    my_alias filename is equivalent to



    my_cmd option1 option2 filename --flag1










    share|improve this question

























      2












      2








      2







      Lets say I have a bash command with a couple of options and the variable that I am interested in (e.g. filename):



      my_cmd option1 option2 filename


      I created an alias:



      alias my_cmd_12="my_cmd option1 option2"


      this allows me to remove typing all of the options. However there are some flags coming after the variable I am interested (e.g. filename):



      my_cmd option1 option2 filename --flag1


      How do I create an alias that takes all option an flags:



      my_alias filename is equivalent to



      my_cmd option1 option2 filename --flag1










      share|improve this question













      Lets say I have a bash command with a couple of options and the variable that I am interested in (e.g. filename):



      my_cmd option1 option2 filename


      I created an alias:



      alias my_cmd_12="my_cmd option1 option2"


      this allows me to remove typing all of the options. However there are some flags coming after the variable I am interested (e.g. filename):



      my_cmd option1 option2 filename --flag1


      How do I create an alias that takes all option an flags:



      my_alias filename is equivalent to



      my_cmd option1 option2 filename --flag1







      linux command-line bash






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 2 hours ago









      motam79

      1649




      1649






















          1 Answer
          1






          active

          oldest

          votes


















          3














          You can't do this with alias. Alias works by replacing string with another string. With this alias defined



          alias my_cmd_12="my_cmd option1 option2"


          my_cmd_12 filename --flag1 will expand to



          my_cmd option1 option2 filename --flag1


          But you want to invoke my_alias filename to get the same result. There is no way to replace my_alias with another string so --flag1 appears at the end.



          However a function should work:



          my_function() { my_cmd option1 option2 "$1" --flag1; }


          Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@", conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.



          More information here: In Bash, when to alias, when to script, and when to write a function?






          share|improve this answer





















          • For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
            – Hannu
            57 mins ago













          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          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%2fsuperuser.com%2fquestions%2f1387609%2fhow-to-create-alias-that-takes-a-positional-argument-in-linux-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









          3














          You can't do this with alias. Alias works by replacing string with another string. With this alias defined



          alias my_cmd_12="my_cmd option1 option2"


          my_cmd_12 filename --flag1 will expand to



          my_cmd option1 option2 filename --flag1


          But you want to invoke my_alias filename to get the same result. There is no way to replace my_alias with another string so --flag1 appears at the end.



          However a function should work:



          my_function() { my_cmd option1 option2 "$1" --flag1; }


          Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@", conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.



          More information here: In Bash, when to alias, when to script, and when to write a function?






          share|improve this answer





















          • For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
            – Hannu
            57 mins ago


















          3














          You can't do this with alias. Alias works by replacing string with another string. With this alias defined



          alias my_cmd_12="my_cmd option1 option2"


          my_cmd_12 filename --flag1 will expand to



          my_cmd option1 option2 filename --flag1


          But you want to invoke my_alias filename to get the same result. There is no way to replace my_alias with another string so --flag1 appears at the end.



          However a function should work:



          my_function() { my_cmd option1 option2 "$1" --flag1; }


          Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@", conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.



          More information here: In Bash, when to alias, when to script, and when to write a function?






          share|improve this answer





















          • For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
            – Hannu
            57 mins ago
















          3












          3








          3






          You can't do this with alias. Alias works by replacing string with another string. With this alias defined



          alias my_cmd_12="my_cmd option1 option2"


          my_cmd_12 filename --flag1 will expand to



          my_cmd option1 option2 filename --flag1


          But you want to invoke my_alias filename to get the same result. There is no way to replace my_alias with another string so --flag1 appears at the end.



          However a function should work:



          my_function() { my_cmd option1 option2 "$1" --flag1; }


          Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@", conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.



          More information here: In Bash, when to alias, when to script, and when to write a function?






          share|improve this answer












          You can't do this with alias. Alias works by replacing string with another string. With this alias defined



          alias my_cmd_12="my_cmd option1 option2"


          my_cmd_12 filename --flag1 will expand to



          my_cmd option1 option2 filename --flag1


          But you want to invoke my_alias filename to get the same result. There is no way to replace my_alias with another string so --flag1 appears at the end.



          However a function should work:



          my_function() { my_cmd option1 option2 "$1" --flag1; }


          Note this is just a minimal solution tailored to your example. In general you can use more positional parameters or "$@", conditional statements etc., according to what exactly you need. Functions are way more flexible than aliases.



          More information here: In Bash, when to alias, when to script, and when to write a function?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 1 hour ago









          Kamil Maciorowski

          23.6k155074




          23.6k155074












          • For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
            – Hannu
            57 mins ago




















          • For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
            – Hannu
            57 mins ago


















          For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
          – Hannu
          57 mins ago






          For general "get to know bash better" - tldp.org -> guides -> look for "Bash", there are at least two that are worth the effort to read.
          – Hannu
          57 mins ago




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • 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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2fsuperuser.com%2fquestions%2f1387609%2fhow-to-create-alias-that-takes-a-positional-argument-in-linux-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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          TypeError: fit_transform() missing 1 required positional argument: 'X'