run two commands in one windows cmd line, one command is SET command

前端 未结 2 1021
夕颜
夕颜 2021-01-02 17:36

[purpose]

This simple command sequence runs expected in the Windows\' CMD shell:

dir & echo hello

will list the files and direct

2条回答
  •  离开以前
    2021-01-02 18:21

    You can also use cmd /V /C (with /V to enable delayed expansion).
    That is great to set an environment variable for just one command in Windows cmd.exe:

    cmd /V /C "set "name=value" && echo !name!"
    value
    

    Note the usage of double-quotes in set "name=value" to avoid the extra space after value.
    For instance, without double-quotes:

    cmd /V /C "set name=value && echo '!name!'"
    'value '
    

    You would need to think to remove the space between value and &&:

    cmd /V /C "set name=value&& echo '!name!'"
    'value'
    

    But using double-quotes makes the assignment more explicit.

提交回复
热议问题