117

When working with Bash, I can put the output of one command into another command like so:

my_command `echo Test`

would be the same thing as

my_command Test

(Obviously, this is just a non-practical example.)

I'm just wondering if you can do the same thing in Batch.

Pascal Cuoq
  • 79,187
  • 7
  • 161
  • 281
MiffTheFox
  • 21,302
  • 14
  • 69
  • 94
  • possible duplicate of [How do I get the result of a command in a variable in windows?](http://stackoverflow.com/questions/108439/how-do-i-get-the-result-of-a-command-in-a-variable-in-windows) – Cristian Ciupitu Jul 01 '14 at 16:55
  • Related https://stackoverflow.com/q/434038/6064933 – jdhao Feb 20 '21 at 02:39

5 Answers5

117

You can get a similar functionality using cmd.exe scripts with the for /f command:

for /f "usebackq tokens=*" %%a in (`echo Test`) do my_command %%a

Yeah, it's kinda non-obvious (to say the least), but it's what's there.

See for /? for the gory details.

Sidenote: I thought that to use "echo" inside the backticks in a "for /f" command would need to be done using "cmd.exe /c echo Test" since echo is an internal command to cmd.exe, but it works in the more natural way. Windows batch scripts always surprise me somehow (but not usually in a good way).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Well, everything in that line is executed by `cmd` anyway; why would you need to spawn a subshell for commands that are perfectly legal there already? – Joey May 04 '10 at 21:27
  • @Johannes: I seem to remember situations where trying to execute internal commands had to be done by specifying `cmd.exe` or `command.com` (this might be some dark memory from MS-DOS days), but I don't recall the details - I might even be entirely mistaken. I was mildly surprised (but not greatly surprised) when `echo` worked in this case without spawning `cmd.exe`. Maybe I'm getting confused by times I had to configure other programs to execute internal commands. I should probably just get rid of the note. – Michael Burr May 04 '10 at 22:16
  • 2
    You usually have to do this when executing shell-builtins from external programs that don't automatically spawn a shell. I.e. C's `system()` was fine, iirc, since it starts a shell in any case but .NET's `Process.Start` needs to explicitly invoke the shell. Something like that, iirc. In any case, I consider this to be the better answer than the accepted one :-) – Joey May 04 '10 at 22:19
  • 2
    The options to `for` don't hurt but confuse the issue. What about this: `for /f %%a in ('"echo Test"') do my_command %%a` – Evan Haas Aug 01 '12 at 18:22
  • 14
    Use a single % instead of %% when executing the same on the command line, rather than in a batch script. – jsears Feb 11 '14 at 02:00
  • 2
    Doesn't seem to work well with piping, for example the ">" operator. But otherwise this is awesome. Unless this was just a % vs %% issue i had. – Jonathan Apr 21 '14 at 21:09
  • 4
    @Evan: `tokens=*` is important. Otherwise, if the result of the command includes spaces, only the first word is output. Run this at the prompt: `for /f %a in ('"echo foo bar"') do echo "%a"`. It will print `"foo"`. – Dan Dascalescu Nov 28 '14 at 05:59
  • 1
    Also worth noting [this answer](https://stackoverflow.com/questions/20841622#20841821) if trying to use pipe symbols etc. in the command. – Steve Chambers Nov 14 '17 at 14:27
  • Node that you can also do the inverse: `for /f "usebackq delims=" %%a in (\`cmd\`)`. – thdoan May 23 '18 at 10:50
  • @MichaelBurr, your intuition about needing a subshell is close to correct. A `FOR /f` loop is designed to run a child process and read standard output from a pipe. For an internal command such as `ECHO`, in this case CMD implicitly executes it via `cmd.exe /c`. You can confirm this by attaching a debugger. – Eryk Sun Oct 13 '19 at 16:15
  • ``$ for /f "usebackq tokens=*" %%a in (`echo Test`) do dir %%a`` returns `%%a was unexpected at this time`. How is this supposed to work? – nephewtom May 05 '20 at 11:22
67

You can do it by redirecting the output to a file first. For example:

echo zz > bla.txt
set /p VV=<bla.txt
echo %VV%
Joey
  • 344,408
  • 85
  • 689
  • 683
zvrba
  • 24,186
  • 3
  • 55
  • 65
  • 19
    It requires you to find a place where you have write access to store the temporary file; you have to clean up after yourself; this very example only enables you to read the very first line of input. For all practical purposes the `for /f` variant is a much better one. – Joey May 04 '10 at 21:33
  • @grawity - along with `%random%` – bacar Aug 26 '11 at 11:34
  • 1
    @joey It does read each line, but how to concatenate to a command line argument in a *single* command. – user877329 Mar 19 '14 at 16:47
  • 1
    how does it handle newline(s)? – andrewrk Oct 16 '17 at 13:43
36

Read the documentation for the "for" command: for /?

Sadly I'm not logged in to Windows to check it myself, but I think something like this can approximate what you want:

for /F %i in ('echo Test') do my_command %i
Weeble
  • 17,058
  • 3
  • 60
  • 75
  • 10
    In case someone else stumbles on this, when the command is executed from a batch file (*.bat), both %i variables need to be double percent sign: %%i. – mgouin Jun 02 '17 at 14:28
4

Maybe I'm screwing up the syntax of the standard for /f method, but when I put a very complex command involving && and | within the backticks in the limit of the for /f, it causes problems. A slight modification from the usual is possible to handle an arbitrary complexity command:

SET VV=some_command -many -arguments && another_command -requiring -the-other -command | handling_of_output | more_handling
for /f "usebackq tokens=*" %%a in (`%VV%`) do mycommand %%a

By putting your full and complex command in a variable first, then putting a reference to the variable in the limit rather than putting the complex command directly into the limit of the for loop, you can avoid syntax interpretation issues. Currently if I copy the exact command I have set to the VV variable in the example above into where it's used, %VV%, it causes syntax errors.

zb226
  • 9,586
  • 6
  • 49
  • 79
mtalexan
  • 677
  • 1
  • 7
  • 17
  • 5
    This didn't work for me, but I found that you can do piping directly within the for command if you escape it with a carat (^): for /f "usebackq tokens=*" %%a in (\`command ^| command\`) do command %%a – Heptite Jan 26 '15 at 02:16
  • @Heptite Card escaping didn't work for me, but surrounding command line with double quotes did. – Alexandr Zarubkin Sep 09 '16 at 13:35
  • @Heptite In my case I didn't know the series of commands I was going to need to run beforehand, they were extracted out of a file in one case, and passed in in another. With the escaping you need to know how many times to escape the command based on how many interpreters it's going to run through, and in my use cases that wasn't a consistent number of interpreters. – mtalexan Dec 06 '17 at 01:44
2

You could always run Bash inside Windows. I do it all the time with MSYS (much more efficient than Cygwin).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
davr
  • 18,877
  • 17
  • 76
  • 99
  • Unfortunately MSYS is barely maintained now, to get an up-to-date bash you need to install it separately – Ed Randall Apr 14 '15 at 06:48
  • 1
    BusyBox is even smaller (~432KB). Not a full Bash though, just Ash. Get the inofficial Windows port here: http://frippery.org/busybox – Martin Dec 14 '15 at 16:09