Passing output from a DOS command as arguments to another command?












2














In linux, this takes one command, and passes its output (stdout) to another:



echo `ls`


What is the Windows/cmd/batch equivalent?










share|improve this question



























    2














    In linux, this takes one command, and passes its output (stdout) to another:



    echo `ls`


    What is the Windows/cmd/batch equivalent?










    share|improve this question

























      2












      2








      2


      2





      In linux, this takes one command, and passes its output (stdout) to another:



      echo `ls`


      What is the Windows/cmd/batch equivalent?










      share|improve this question













      In linux, this takes one command, and passes its output (stdout) to another:



      echo `ls`


      What is the Windows/cmd/batch equivalent?







      windows command-line






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 9 '12 at 16:06









      ripper234

      4,3532973105




      4,3532973105






















          2 Answers
          2






          active

          oldest

          votes


















          12














          cmd.exe offers a rudimentary command substitution facility using backticks, but only via the for loop with the /f usebackq option. It's pretty brain-dead compared to what a Unix shell would do, however, as its standard behavior is to pick up only the first word of each line of output from the child and only so you can iterate over them, not so you can paste all of them at once onto the command line. (You can adjust the behavior somewhat with the delims option.) Here's an example:



          for /f usebackq %F in (`dir /w s*c`) do echo %F


          If you want genuine Unix-style command substitution, you'll need a genuine Unix shell, e.g., either Cygwin bash or my own (commercial) Hamilton C shell.






          share|improve this answer































            0














            Example of echo output of dir (use single quotes, not backtick !):



            echo off & for /f "delims=" %A in ('dir') do echo %A


            In interactive mode, you need type echo on and press Enter to exit. If you are sure that loop only run once (i.e. output of ('xxx') is single line), then you can direct append echo on in the command.



            echo off & for /f "delims=" %A in ('set /a 0x3DE1') do net helpmsg %A & echo on


            For unknown reason, ('set /a 0x3DE1') can't do like 'set /a c=0x3DE1', even though set /a c=0x3DE1 is works as single command.



            Example of echo hello world from 'echo hello world' (single quotes):



            echo off & for /f "delims=" %A in ('echo hello world') do echo %A


            Example of echo hello world from "" (literal string):



            echo off & for /f "delims=" %A in ("hello world") do echo %A


            Above syntax %A is for interactive mode, you need double % in batch file, e.g. %%A



            Take care if you do echo %A inside the loop, imagine if one of the line from output of command called on, then there are 2 problems:



            [1] echo $A means echo on which will echo nothing and invisible. I can only think of comparing the on to handle such specific case.



            [2] echo $A means echo on which will toggle back the command prompt output; You might need add extra echo off inside the loop., e.g.:



            echo off & for /f "delims=" %A in ('dir /b') do echo %A & echo off


            Note that above examples is far from equivalent to `` since it run argument line by line. You may create this batch to try to simulate echo `ls` (it loop and append all the lines into single variable separate by space and then pass as argument later):



            @echo off
            Setlocal EnableDelayedExpansion
            set fls=
            for /f "delims=" %%A in ('dir /b') do set fls=!fls! %%A
            echo %fls%


            -- edit : added missing spaces






            share|improve this answer























              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%2f516999%2fpassing-output-from-a-dos-command-as-arguments-to-another-command%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









              12














              cmd.exe offers a rudimentary command substitution facility using backticks, but only via the for loop with the /f usebackq option. It's pretty brain-dead compared to what a Unix shell would do, however, as its standard behavior is to pick up only the first word of each line of output from the child and only so you can iterate over them, not so you can paste all of them at once onto the command line. (You can adjust the behavior somewhat with the delims option.) Here's an example:



              for /f usebackq %F in (`dir /w s*c`) do echo %F


              If you want genuine Unix-style command substitution, you'll need a genuine Unix shell, e.g., either Cygwin bash or my own (commercial) Hamilton C shell.






              share|improve this answer




























                12














                cmd.exe offers a rudimentary command substitution facility using backticks, but only via the for loop with the /f usebackq option. It's pretty brain-dead compared to what a Unix shell would do, however, as its standard behavior is to pick up only the first word of each line of output from the child and only so you can iterate over them, not so you can paste all of them at once onto the command line. (You can adjust the behavior somewhat with the delims option.) Here's an example:



                for /f usebackq %F in (`dir /w s*c`) do echo %F


                If you want genuine Unix-style command substitution, you'll need a genuine Unix shell, e.g., either Cygwin bash or my own (commercial) Hamilton C shell.






                share|improve this answer


























                  12












                  12








                  12






                  cmd.exe offers a rudimentary command substitution facility using backticks, but only via the for loop with the /f usebackq option. It's pretty brain-dead compared to what a Unix shell would do, however, as its standard behavior is to pick up only the first word of each line of output from the child and only so you can iterate over them, not so you can paste all of them at once onto the command line. (You can adjust the behavior somewhat with the delims option.) Here's an example:



                  for /f usebackq %F in (`dir /w s*c`) do echo %F


                  If you want genuine Unix-style command substitution, you'll need a genuine Unix shell, e.g., either Cygwin bash or my own (commercial) Hamilton C shell.






                  share|improve this answer














                  cmd.exe offers a rudimentary command substitution facility using backticks, but only via the for loop with the /f usebackq option. It's pretty brain-dead compared to what a Unix shell would do, however, as its standard behavior is to pick up only the first word of each line of output from the child and only so you can iterate over them, not so you can paste all of them at once onto the command line. (You can adjust the behavior somewhat with the delims option.) Here's an example:



                  for /f usebackq %F in (`dir /w s*c`) do echo %F


                  If you want genuine Unix-style command substitution, you'll need a genuine Unix shell, e.g., either Cygwin bash or my own (commercial) Hamilton C shell.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Dec 9 '12 at 17:55

























                  answered Dec 9 '12 at 17:36









                  Nicole Hamilton

                  8,43411236




                  8,43411236

























                      0














                      Example of echo output of dir (use single quotes, not backtick !):



                      echo off & for /f "delims=" %A in ('dir') do echo %A


                      In interactive mode, you need type echo on and press Enter to exit. If you are sure that loop only run once (i.e. output of ('xxx') is single line), then you can direct append echo on in the command.



                      echo off & for /f "delims=" %A in ('set /a 0x3DE1') do net helpmsg %A & echo on


                      For unknown reason, ('set /a 0x3DE1') can't do like 'set /a c=0x3DE1', even though set /a c=0x3DE1 is works as single command.



                      Example of echo hello world from 'echo hello world' (single quotes):



                      echo off & for /f "delims=" %A in ('echo hello world') do echo %A


                      Example of echo hello world from "" (literal string):



                      echo off & for /f "delims=" %A in ("hello world") do echo %A


                      Above syntax %A is for interactive mode, you need double % in batch file, e.g. %%A



                      Take care if you do echo %A inside the loop, imagine if one of the line from output of command called on, then there are 2 problems:



                      [1] echo $A means echo on which will echo nothing and invisible. I can only think of comparing the on to handle such specific case.



                      [2] echo $A means echo on which will toggle back the command prompt output; You might need add extra echo off inside the loop., e.g.:



                      echo off & for /f "delims=" %A in ('dir /b') do echo %A & echo off


                      Note that above examples is far from equivalent to `` since it run argument line by line. You may create this batch to try to simulate echo `ls` (it loop and append all the lines into single variable separate by space and then pass as argument later):



                      @echo off
                      Setlocal EnableDelayedExpansion
                      set fls=
                      for /f "delims=" %%A in ('dir /b') do set fls=!fls! %%A
                      echo %fls%


                      -- edit : added missing spaces






                      share|improve this answer




























                        0














                        Example of echo output of dir (use single quotes, not backtick !):



                        echo off & for /f "delims=" %A in ('dir') do echo %A


                        In interactive mode, you need type echo on and press Enter to exit. If you are sure that loop only run once (i.e. output of ('xxx') is single line), then you can direct append echo on in the command.



                        echo off & for /f "delims=" %A in ('set /a 0x3DE1') do net helpmsg %A & echo on


                        For unknown reason, ('set /a 0x3DE1') can't do like 'set /a c=0x3DE1', even though set /a c=0x3DE1 is works as single command.



                        Example of echo hello world from 'echo hello world' (single quotes):



                        echo off & for /f "delims=" %A in ('echo hello world') do echo %A


                        Example of echo hello world from "" (literal string):



                        echo off & for /f "delims=" %A in ("hello world") do echo %A


                        Above syntax %A is for interactive mode, you need double % in batch file, e.g. %%A



                        Take care if you do echo %A inside the loop, imagine if one of the line from output of command called on, then there are 2 problems:



                        [1] echo $A means echo on which will echo nothing and invisible. I can only think of comparing the on to handle such specific case.



                        [2] echo $A means echo on which will toggle back the command prompt output; You might need add extra echo off inside the loop., e.g.:



                        echo off & for /f "delims=" %A in ('dir /b') do echo %A & echo off


                        Note that above examples is far from equivalent to `` since it run argument line by line. You may create this batch to try to simulate echo `ls` (it loop and append all the lines into single variable separate by space and then pass as argument later):



                        @echo off
                        Setlocal EnableDelayedExpansion
                        set fls=
                        for /f "delims=" %%A in ('dir /b') do set fls=!fls! %%A
                        echo %fls%


                        -- edit : added missing spaces






                        share|improve this answer


























                          0












                          0








                          0






                          Example of echo output of dir (use single quotes, not backtick !):



                          echo off & for /f "delims=" %A in ('dir') do echo %A


                          In interactive mode, you need type echo on and press Enter to exit. If you are sure that loop only run once (i.e. output of ('xxx') is single line), then you can direct append echo on in the command.



                          echo off & for /f "delims=" %A in ('set /a 0x3DE1') do net helpmsg %A & echo on


                          For unknown reason, ('set /a 0x3DE1') can't do like 'set /a c=0x3DE1', even though set /a c=0x3DE1 is works as single command.



                          Example of echo hello world from 'echo hello world' (single quotes):



                          echo off & for /f "delims=" %A in ('echo hello world') do echo %A


                          Example of echo hello world from "" (literal string):



                          echo off & for /f "delims=" %A in ("hello world") do echo %A


                          Above syntax %A is for interactive mode, you need double % in batch file, e.g. %%A



                          Take care if you do echo %A inside the loop, imagine if one of the line from output of command called on, then there are 2 problems:



                          [1] echo $A means echo on which will echo nothing and invisible. I can only think of comparing the on to handle such specific case.



                          [2] echo $A means echo on which will toggle back the command prompt output; You might need add extra echo off inside the loop., e.g.:



                          echo off & for /f "delims=" %A in ('dir /b') do echo %A & echo off


                          Note that above examples is far from equivalent to `` since it run argument line by line. You may create this batch to try to simulate echo `ls` (it loop and append all the lines into single variable separate by space and then pass as argument later):



                          @echo off
                          Setlocal EnableDelayedExpansion
                          set fls=
                          for /f "delims=" %%A in ('dir /b') do set fls=!fls! %%A
                          echo %fls%


                          -- edit : added missing spaces






                          share|improve this answer














                          Example of echo output of dir (use single quotes, not backtick !):



                          echo off & for /f "delims=" %A in ('dir') do echo %A


                          In interactive mode, you need type echo on and press Enter to exit. If you are sure that loop only run once (i.e. output of ('xxx') is single line), then you can direct append echo on in the command.



                          echo off & for /f "delims=" %A in ('set /a 0x3DE1') do net helpmsg %A & echo on


                          For unknown reason, ('set /a 0x3DE1') can't do like 'set /a c=0x3DE1', even though set /a c=0x3DE1 is works as single command.



                          Example of echo hello world from 'echo hello world' (single quotes):



                          echo off & for /f "delims=" %A in ('echo hello world') do echo %A


                          Example of echo hello world from "" (literal string):



                          echo off & for /f "delims=" %A in ("hello world") do echo %A


                          Above syntax %A is for interactive mode, you need double % in batch file, e.g. %%A



                          Take care if you do echo %A inside the loop, imagine if one of the line from output of command called on, then there are 2 problems:



                          [1] echo $A means echo on which will echo nothing and invisible. I can only think of comparing the on to handle such specific case.



                          [2] echo $A means echo on which will toggle back the command prompt output; You might need add extra echo off inside the loop., e.g.:



                          echo off & for /f "delims=" %A in ('dir /b') do echo %A & echo off


                          Note that above examples is far from equivalent to `` since it run argument line by line. You may create this batch to try to simulate echo `ls` (it loop and append all the lines into single variable separate by space and then pass as argument later):



                          @echo off
                          Setlocal EnableDelayedExpansion
                          set fls=
                          for /f "delims=" %%A in ('dir /b') do set fls=!fls! %%A
                          echo %fls%


                          -- edit : added missing spaces







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Dec 13 '18 at 16:24









                          Community

                          1




                          1










                          answered Aug 27 '17 at 0:07









                          林果皞

                          301515




                          301515






























                              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%2f516999%2fpassing-output-from-a-dos-command-as-arguments-to-another-command%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

                              Paul Cézanne

                              UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

                              Angular material date-picker (MatDatepicker) auto completes the date on focus out