Windows command prompt: Using a variable set in the same line of a one-liner

后端 未结 4 1248
别那么骄傲
别那么骄傲 2021-02-13 23:29

Okay, I learned that one can use & or && to combine multiple commands into a single line, but it seems that variables that are set

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-14 00:06

    Try the following:

    cmd.exe /v /c "set foo=hello, world & echo !foo!"
    

    The /v argument enables delayed variable expansion. This allows you to access variables' values at execution time rather than at parse time (the default). You do this by writing !foo! instead of %foo% (with this /v option turned on).

    Instead of passing /v, you can also turn delayed variable expansion on permanently via the registry, which obviously affects the entire system:

    [HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor]
    "DelayedExpansion"= (REG_DWORD)
    1=enabled 0=disabled (default)
    

提交回复
热议问题