Using >nul in a variable

泄露秘密 提交于 2020-01-24 19:50:21

问题


Does anyone know how to stop >nul being ignored when set in a variable, or is this not possible? I have a feeling that it's one of those things that will only work once all variables have been expanded or something, but it can't hurt to ask.

Example:

@echo off

:: Won't work
SET pause_5=ping localhost /n 6 >nul
%pause_5%

:: Will work
SET pause_5=ping localhost /n 6
%pause_5% >nul

exit

回答1:


Put quotes around the argument:

set "pause_5=ping localhost /n 6 >nul"

Another option is to escape characters that will be interpreted by the shell:

set pause_5=ping localhost /n 6 ^>nul

But usually the quotes approach is a lot easier.

The way you wrote is essentially said »set pause_5 to ping localhost /n 6 and ignore the output of the set command.«.



来源:https://stackoverflow.com/questions/11686038/using-nul-in-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!