Run a command using arguments that come from an array

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











up vote
3
down vote

favorite












Suppose I have a graphical program named app. Usage example: app -t 'first tab' -t 'second tab' opens two 'tabs' in that program.



The question is: how can I execute the command (i.e. app) from within a bash script if the number of arguments can vary?



Consider this:



#!/bin/bash
tabs=(
'first tab'
'second tab'
)

# Open the app (starting with some tabs).
app  # ... How to get `app -t 'first tab' -t 'second tab'`?


I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'. How can such a bash script be written?



Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.







share|improve this question


























    up vote
    3
    down vote

    favorite












    Suppose I have a graphical program named app. Usage example: app -t 'first tab' -t 'second tab' opens two 'tabs' in that program.



    The question is: how can I execute the command (i.e. app) from within a bash script if the number of arguments can vary?



    Consider this:



    #!/bin/bash
    tabs=(
    'first tab'
    'second tab'
    )

    # Open the app (starting with some tabs).
    app  # ... How to get `app -t 'first tab' -t 'second tab'`?


    I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'. How can such a bash script be written?



    Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.







    share|improve this question
























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      Suppose I have a graphical program named app. Usage example: app -t 'first tab' -t 'second tab' opens two 'tabs' in that program.



      The question is: how can I execute the command (i.e. app) from within a bash script if the number of arguments can vary?



      Consider this:



      #!/bin/bash
      tabs=(
      'first tab'
      'second tab'
      )

      # Open the app (starting with some tabs).
      app  # ... How to get `app -t 'first tab' -t 'second tab'`?


      I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'. How can such a bash script be written?



      Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.







      share|improve this question














      Suppose I have a graphical program named app. Usage example: app -t 'first tab' -t 'second tab' opens two 'tabs' in that program.



      The question is: how can I execute the command (i.e. app) from within a bash script if the number of arguments can vary?



      Consider this:



      #!/bin/bash
      tabs=(
      'first tab'
      'second tab'
      )

      # Open the app (starting with some tabs).
      app  # ... How to get `app -t 'first tab' -t 'second tab'`?


      I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'. How can such a bash script be written?



      Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.









      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 24 '17 at 4:37

























      asked Dec 23 '17 at 8:25









      Flux

      1645




      1645




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          5
          down vote



          accepted










          Giving the arguments from an array is easy, "$array[@]" expands to the array entries as distinct words (arguments). We just need to add the -t flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t flags as we go:



          #!/bin/bash
          tabs=("first tab" "second tab")
          args=()
          for t in "$tabs[@]" ; do
          args+=(-t "$t")
          done
          app "$args[@]"


          Use "$@" instead of "$tabs[@]" to take the command line arguments of the script instead of a hard coded list.






          share|improve this answer



























            up vote
            3
            down vote













            tabs=("-t" "one tab" "-t" "second tab")
            echo app "$tabs[@]"
            app -t one tab -t second tab


            So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.






            share|improve this answer



























              up vote
              0
              down vote













              It's easier with zsh:



              #!/bin/zsh -
              tabs=(
              'first tab'
              'second tab'
              )

              app -t$^tabs


              That calls app as if you had entered:



              app -t'first tab' -t'second tab'


              rc, es and fish do that implicitly (zsh's $^array is actually inspired from rc's ^):



              In those shells,



              app -t$tabs


              would do that as well.



              To call app as if with



              app -t 'first tab' -t 'second tab'


              as opposed to



              app -t'first tab' -t'second tab'


              That is where -t and first tab are two different arguments to app, with zsh:



              app $$:--t:^^tabs


              using the $array1:^^array2 array-zipping operator where $:--t (minimal form of $var:-default) is used to inline the first array.






              share|improve this answer



























                up vote
                -1
                down vote













                Basically the following bash script will read the file which include sites line by line and open each site in a new tab.



                #!/bin/bash
                while read line
                do
                xdg-open "$line"
                done < /root/file



                Usage




                ./script.sh sites






                share|improve this answer






















                • Ah but you are running the command (xdg-open in your case) multiple times. I would like the command to be run only once.
                  – Flux
                  Dec 23 '17 at 8:44











                • The question is about getting the arguments from an array, not a file.
                  – Barmar
                  Dec 23 '17 at 11:43










                • @roaima The question has always been about an array. There has never been any mention of files.
                  – Flux
                  Dec 23 '17 at 17:38











                • @Flux sorry yes. Can't think now why I'd have written that. Deleting...
                  – roaima
                  Dec 23 '17 at 18:45











                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%2f412638%2frun-a-command-using-arguments-that-come-from-an-array%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                5
                down vote



                accepted










                Giving the arguments from an array is easy, "$array[@]" expands to the array entries as distinct words (arguments). We just need to add the -t flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t flags as we go:



                #!/bin/bash
                tabs=("first tab" "second tab")
                args=()
                for t in "$tabs[@]" ; do
                args+=(-t "$t")
                done
                app "$args[@]"


                Use "$@" instead of "$tabs[@]" to take the command line arguments of the script instead of a hard coded list.






                share|improve this answer
























                  up vote
                  5
                  down vote



                  accepted










                  Giving the arguments from an array is easy, "$array[@]" expands to the array entries as distinct words (arguments). We just need to add the -t flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t flags as we go:



                  #!/bin/bash
                  tabs=("first tab" "second tab")
                  args=()
                  for t in "$tabs[@]" ; do
                  args+=(-t "$t")
                  done
                  app "$args[@]"


                  Use "$@" instead of "$tabs[@]" to take the command line arguments of the script instead of a hard coded list.






                  share|improve this answer






















                    up vote
                    5
                    down vote



                    accepted







                    up vote
                    5
                    down vote



                    accepted






                    Giving the arguments from an array is easy, "$array[@]" expands to the array entries as distinct words (arguments). We just need to add the -t flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t flags as we go:



                    #!/bin/bash
                    tabs=("first tab" "second tab")
                    args=()
                    for t in "$tabs[@]" ; do
                    args+=(-t "$t")
                    done
                    app "$args[@]"


                    Use "$@" instead of "$tabs[@]" to take the command line arguments of the script instead of a hard coded list.






                    share|improve this answer












                    Giving the arguments from an array is easy, "$array[@]" expands to the array entries as distinct words (arguments). We just need to add the -t flags. To do that, we can loop over the first array, and build another array for the full list of arguments, adding the -t flags as we go:



                    #!/bin/bash
                    tabs=("first tab" "second tab")
                    args=()
                    for t in "$tabs[@]" ; do
                    args+=(-t "$t")
                    done
                    app "$args[@]"


                    Use "$@" instead of "$tabs[@]" to take the command line arguments of the script instead of a hard coded list.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 23 '17 at 9:15









                    ilkkachu

                    49.9k674137




                    49.9k674137






















                        up vote
                        3
                        down vote













                        tabs=("-t" "one tab" "-t" "second tab")
                        echo app "$tabs[@]"
                        app -t one tab -t second tab


                        So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.






                        share|improve this answer
























                          up vote
                          3
                          down vote













                          tabs=("-t" "one tab" "-t" "second tab")
                          echo app "$tabs[@]"
                          app -t one tab -t second tab


                          So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.






                          share|improve this answer






















                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote









                            tabs=("-t" "one tab" "-t" "second tab")
                            echo app "$tabs[@]"
                            app -t one tab -t second tab


                            So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.






                            share|improve this answer












                            tabs=("-t" "one tab" "-t" "second tab")
                            echo app "$tabs[@]"
                            app -t one tab -t second tab


                            So now you should convert your original array to array with "-t" flags. Hope it's not a problem at all.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 23 '17 at 9:02









                            Fedor Dikarev

                            963210




                            963210




















                                up vote
                                0
                                down vote













                                It's easier with zsh:



                                #!/bin/zsh -
                                tabs=(
                                'first tab'
                                'second tab'
                                )

                                app -t$^tabs


                                That calls app as if you had entered:



                                app -t'first tab' -t'second tab'


                                rc, es and fish do that implicitly (zsh's $^array is actually inspired from rc's ^):



                                In those shells,



                                app -t$tabs


                                would do that as well.



                                To call app as if with



                                app -t 'first tab' -t 'second tab'


                                as opposed to



                                app -t'first tab' -t'second tab'


                                That is where -t and first tab are two different arguments to app, with zsh:



                                app $$:--t:^^tabs


                                using the $array1:^^array2 array-zipping operator where $:--t (minimal form of $var:-default) is used to inline the first array.






                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  It's easier with zsh:



                                  #!/bin/zsh -
                                  tabs=(
                                  'first tab'
                                  'second tab'
                                  )

                                  app -t$^tabs


                                  That calls app as if you had entered:



                                  app -t'first tab' -t'second tab'


                                  rc, es and fish do that implicitly (zsh's $^array is actually inspired from rc's ^):



                                  In those shells,



                                  app -t$tabs


                                  would do that as well.



                                  To call app as if with



                                  app -t 'first tab' -t 'second tab'


                                  as opposed to



                                  app -t'first tab' -t'second tab'


                                  That is where -t and first tab are two different arguments to app, with zsh:



                                  app $$:--t:^^tabs


                                  using the $array1:^^array2 array-zipping operator where $:--t (minimal form of $var:-default) is used to inline the first array.






                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    It's easier with zsh:



                                    #!/bin/zsh -
                                    tabs=(
                                    'first tab'
                                    'second tab'
                                    )

                                    app -t$^tabs


                                    That calls app as if you had entered:



                                    app -t'first tab' -t'second tab'


                                    rc, es and fish do that implicitly (zsh's $^array is actually inspired from rc's ^):



                                    In those shells,



                                    app -t$tabs


                                    would do that as well.



                                    To call app as if with



                                    app -t 'first tab' -t 'second tab'


                                    as opposed to



                                    app -t'first tab' -t'second tab'


                                    That is where -t and first tab are two different arguments to app, with zsh:



                                    app $$:--t:^^tabs


                                    using the $array1:^^array2 array-zipping operator where $:--t (minimal form of $var:-default) is used to inline the first array.






                                    share|improve this answer












                                    It's easier with zsh:



                                    #!/bin/zsh -
                                    tabs=(
                                    'first tab'
                                    'second tab'
                                    )

                                    app -t$^tabs


                                    That calls app as if you had entered:



                                    app -t'first tab' -t'second tab'


                                    rc, es and fish do that implicitly (zsh's $^array is actually inspired from rc's ^):



                                    In those shells,



                                    app -t$tabs


                                    would do that as well.



                                    To call app as if with



                                    app -t 'first tab' -t 'second tab'


                                    as opposed to



                                    app -t'first tab' -t'second tab'


                                    That is where -t and first tab are two different arguments to app, with zsh:



                                    app $$:--t:^^tabs


                                    using the $array1:^^array2 array-zipping operator where $:--t (minimal form of $var:-default) is used to inline the first array.







                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 23 '17 at 22:56









                                    Stéphane Chazelas

                                    282k53518851




                                    282k53518851




















                                        up vote
                                        -1
                                        down vote













                                        Basically the following bash script will read the file which include sites line by line and open each site in a new tab.



                                        #!/bin/bash
                                        while read line
                                        do
                                        xdg-open "$line"
                                        done < /root/file



                                        Usage




                                        ./script.sh sites






                                        share|improve this answer






















                                        • Ah but you are running the command (xdg-open in your case) multiple times. I would like the command to be run only once.
                                          – Flux
                                          Dec 23 '17 at 8:44











                                        • The question is about getting the arguments from an array, not a file.
                                          – Barmar
                                          Dec 23 '17 at 11:43










                                        • @roaima The question has always been about an array. There has never been any mention of files.
                                          – Flux
                                          Dec 23 '17 at 17:38











                                        • @Flux sorry yes. Can't think now why I'd have written that. Deleting...
                                          – roaima
                                          Dec 23 '17 at 18:45















                                        up vote
                                        -1
                                        down vote













                                        Basically the following bash script will read the file which include sites line by line and open each site in a new tab.



                                        #!/bin/bash
                                        while read line
                                        do
                                        xdg-open "$line"
                                        done < /root/file



                                        Usage




                                        ./script.sh sites






                                        share|improve this answer






















                                        • Ah but you are running the command (xdg-open in your case) multiple times. I would like the command to be run only once.
                                          – Flux
                                          Dec 23 '17 at 8:44











                                        • The question is about getting the arguments from an array, not a file.
                                          – Barmar
                                          Dec 23 '17 at 11:43










                                        • @roaima The question has always been about an array. There has never been any mention of files.
                                          – Flux
                                          Dec 23 '17 at 17:38











                                        • @Flux sorry yes. Can't think now why I'd have written that. Deleting...
                                          – roaima
                                          Dec 23 '17 at 18:45













                                        up vote
                                        -1
                                        down vote










                                        up vote
                                        -1
                                        down vote









                                        Basically the following bash script will read the file which include sites line by line and open each site in a new tab.



                                        #!/bin/bash
                                        while read line
                                        do
                                        xdg-open "$line"
                                        done < /root/file



                                        Usage




                                        ./script.sh sites






                                        share|improve this answer














                                        Basically the following bash script will read the file which include sites line by line and open each site in a new tab.



                                        #!/bin/bash
                                        while read line
                                        do
                                        xdg-open "$line"
                                        done < /root/file



                                        Usage




                                        ./script.sh sites







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Dec 23 '17 at 18:46









                                        roaima

                                        39.8k545108




                                        39.8k545108










                                        answered Dec 23 '17 at 8:28









                                        αԋɱҽԃ αмєяιcαη

                                        407418




                                        407418











                                        • Ah but you are running the command (xdg-open in your case) multiple times. I would like the command to be run only once.
                                          – Flux
                                          Dec 23 '17 at 8:44











                                        • The question is about getting the arguments from an array, not a file.
                                          – Barmar
                                          Dec 23 '17 at 11:43










                                        • @roaima The question has always been about an array. There has never been any mention of files.
                                          – Flux
                                          Dec 23 '17 at 17:38











                                        • @Flux sorry yes. Can't think now why I'd have written that. Deleting...
                                          – roaima
                                          Dec 23 '17 at 18:45

















                                        • Ah but you are running the command (xdg-open in your case) multiple times. I would like the command to be run only once.
                                          – Flux
                                          Dec 23 '17 at 8:44











                                        • The question is about getting the arguments from an array, not a file.
                                          – Barmar
                                          Dec 23 '17 at 11:43










                                        • @roaima The question has always been about an array. There has never been any mention of files.
                                          – Flux
                                          Dec 23 '17 at 17:38











                                        • @Flux sorry yes. Can't think now why I'd have written that. Deleting...
                                          – roaima
                                          Dec 23 '17 at 18:45
















                                        Ah but you are running the command (xdg-open in your case) multiple times. I would like the command to be run only once.
                                        – Flux
                                        Dec 23 '17 at 8:44





                                        Ah but you are running the command (xdg-open in your case) multiple times. I would like the command to be run only once.
                                        – Flux
                                        Dec 23 '17 at 8:44













                                        The question is about getting the arguments from an array, not a file.
                                        – Barmar
                                        Dec 23 '17 at 11:43




                                        The question is about getting the arguments from an array, not a file.
                                        – Barmar
                                        Dec 23 '17 at 11:43












                                        @roaima The question has always been about an array. There has never been any mention of files.
                                        – Flux
                                        Dec 23 '17 at 17:38





                                        @roaima The question has always been about an array. There has never been any mention of files.
                                        – Flux
                                        Dec 23 '17 at 17:38













                                        @Flux sorry yes. Can't think now why I'd have written that. Deleting...
                                        – roaima
                                        Dec 23 '17 at 18:45





                                        @Flux sorry yes. Can't think now why I'd have written that. Deleting...
                                        – roaima
                                        Dec 23 '17 at 18:45













                                         

                                        draft saved


                                        draft discarded


























                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412638%2frun-a-command-using-arguments-that-come-from-an-array%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?

                                        Displaying single band from multi-band raster using QGIS

                                        How many registers does an x86_64 CPU actually have?