使用Windows批处理文件中的命令结果设置变量的值 [英] Set the value of a variable with the result of a command in a Windows batch file

查看:356
本文介绍了使用Windows批处理文件中的命令结果设置变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bash 环境中工作时,我通常会执行以下操作来设置变量的值作为命令的结果:

When working in a Bash environment, to set the value of a variable as the result of a command, I usually do:

var=$(command -args)

其中,var是由命令command -args设置的变量.然后,我可以作为$var来访问该变量.

where var is the variable set by the command command -args. I can then access that variable as $var.

与几乎所有Unix shell兼容的更常规的实现方法是:

A more conventional way to do this which is compatible with almost every Unix shell is:

set var=`command -args`

也就是说,如何在 Windows批处理文件中使用命令的结果设置变量的值?我尝试过:

That said, how can I set the value of a variable with the result of a command in a Windows batch file? I've tried:

set var=command -args

但是我发现var设置为command -args而不是命令的输出.

But I find that var is set to command -args rather than the output of the command.

推荐答案

从Windows批处理文件执行 Jesse描述的您将需要写:

To do what Jesse describes, from a Windows batch file you will need to write:

for /f "delims=" %%a in ('ver') do @set foobar=%%a 

但是,如果您习惯使用Unix类型的脚本,我建议在Windows系统上使用 Cygwin .

But, I instead suggest using Cygwin on your Windows system if you are used to Unix-type scripting.

这篇关于使用Windows批处理文件中的命令结果设置变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆