Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch equivalent of Bash backticks

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.

like image 262
MiffTheFox Avatar asked May 04 '10 20:05

MiffTheFox


People also ask

What does %% mean in batch script?

Represents a replaceable parameter. Use a single percent sign ( % ) to carry out the for command at the command prompt. Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> )

What is NX in batch?

This means that your PATH is not defined correctly. Any NX command line application should be launched from a NX Command Prompt to ensure that the environment is set up correctly. Use Start, All Programs, Siemens NX##, Tools, Command Prompt to open a NX command prompt.


2 Answers

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

like image 116
Michael Burr Avatar answered Oct 19 '22 00:10

Michael Burr


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% 
like image 24
zvrba Avatar answered Oct 19 '22 01:10

zvrba