Windows命令提示符:在单行的同一行中使用变量集 [英] Windows command prompt: Using a variable set in the same line of a one-liner

查看:191
本文介绍了Windows命令提示符:在单行的同一行中使用变量集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我了解到可以使用&&&将多个命令组合到一行中,但是看来set的变量实际上并不能在同一行中进行插值:

Okay, I learned that one can use & or && to combine multiple commands into a single line, but it seems that variables that are set aren't actually available for interpolation in the same line:

C:\Users\Andrew>set foo=hello, world!&& echo %foo%
%foo%

C:\Users\Andrew>echo %foo%
hello, world!

为什么我不能进行这项工作,并且有什么方法可以使它在一行中工作?

我需要单行代码的原因是,我正在使用的外部程序接受一个命令作为预运行挂钩,当然,我需要运行多个命令.

The reason I need a one-liner is that an external program I'm working with accepts a single command as a pre-run hook, and, of course, I need to run multiple commands.

  1. "hello, world!应该用双引号引起来!" 实际上,这样做似乎将文字双引号存储在变量中,我不希望这样做,例如

  1. "hello, world! should be surrounded in double-quotes!" Actually, doing so seems to store literal double-quotes in the variable, which I do not want, e.g.

C:\Users\Andrew>set bar="hello, world!"&& echo %bar%
%bar%

C:\Users\Andrew>echo %bar%
"hello, world!"

  • "&&之前应该有一个空格!" 实际上,这样做似乎在变量中存储了尾部空格,我不希望这样做,例如 eg

  • "There should be a space before the &&!" Actually, doing so seems to store a trailing space in the variable, which I do not want, e.g.

    C:\Users\Andrew>set bar="hello, world!"&& echo %bar%
    %bar%
    
    C:\Users\Andrew>echo %bar%
    "hello, world!"
    

  • 都是!" >:(

    C:\Users\Andrew>set mu="hello, world!" && echo %mu%
    %mu%
    
    C:\Users\Andrew>echo (%mu%)
    ("hello, world!" )
    

  • 推荐答案

    您可以在同一行中执行此操作,但是我建议您使用 preHook.bat 之类的批处理文件.

    You can do it in the same line, but I would recommend to use a batch file like preHook.bat.

    作为一个班轮

    set "mu=hello, world!" && call echo %^mu%
    

    起初,您会发现报价与您的报价不同.

    At first you can see that the quotes are different than yours.

    这是为什么:

    set test1="quotes"
    set "test2=no quotes" with comment
    

    在第一个测试中,引号将是test1变量的一部分,以及最后一个引号之后的所有字符.

    In the first test, the quotes will be part of the test1 variable, as well as all characters after the last quote.

    在第二个测试中,它使用SET命令的扩展语法.
    因此,内容将为no quotes,因为仅使用最后一个引号的内容.其余的将被丢弃.

    In the second test, it uses the extended syntax of the SET command.
    So the content will be no quotes, as only the content to the last quote is used; the rest will be dropped.

    要回显变量内容,我使用call echo %^mu%,因为在执行任何命令之前,在解析行时,百分比扩展将扩展它.

    To echo the variable content, I use call echo %^mu%, as percent expansion will expand it when the line is parsed, before any of the commands are executed.

    但是call命令将在以后执行,并重新启动解析器,该解析器在命令行中使用时,将使用与批处理解析器不同的扩展规则:空变量(在这种情况下为%^mu%,第一次)在该行中保持不变;但是,在下一个解析器阶段,将删除^插入符号.

    But the call command will be executed later, and it restarts the parser, which, when used at the command line, uses different expansion rules than the batch parser: an empty variable (in this case, %^mu%, in the first time) stays unchanged in the line; but, in the next parser phase, the ^ caret will be removed.

    在您的情况下,call echo %mu%也将起作用,但仅当mu始终为空时.当mu在执行该行之前有内容时,插入符号变体也可以使用.

    In your case, call echo %mu% would also work, but only when mu is always empty. The caret variant also works when mu has content before the line is executed.

    有关解析器的更多信息,请参见 SO:Windows命令解释器(CMD.EXE)如何解析脚本? 关于变量扩展,请访问 SO:变量扩展

    More about the parser at SO: How does the Windows Command Interpreter (CMD.EXE) parse scripts? And about variable expansion at SO: Variable expansion

    这篇关于Windows命令提示符:在单行的同一行中使用变量集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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