开发者

Is it possible to set an environment variable to the output of a command in cmd.exe

开发者 https://www.devze.com 2023-01-06 20:17 出处:网络
I need to do the equivalent of set ENVAR=`some-command` In a windows/cmd.exe script.Cygwin is not an option.

I need to do the equivalent of

set ENVAR=`some-command`

In a windows/cmd.exe script. Cygwin is not an option.

开发者_JAVA百科

For bonus marks: Is there some cmd.exe equivalent of backticks in general?


A quick and dirty way would be redirecting it to a file and then reading this, e.g.

some-command>out.txt
set /p ENVAR=<out.txt

I think for can also help you, but I don't remember the exact syntax. Try something like

for /f "usebackq" %x in (`some-command`) do set ENVAR=%x

I probably forgot some token or delim in the options...


Not "probably", it is absolutely a must to specify "delims=" (it means "no delimiters"), unless you want your variable to only contain up to first space or tab of the input data.

It is recommended to specify "delims=" as the last option to avoid potential confusion in options perception by the operator and by the shell.

I.e.

FOR /F "usebackq delims=" %%a IN (`cygpath.exe -u "%~1"`) DO (
    SET CMDNAME=%%~a
    SHIFT
)

See SS64 article on FOR /F.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号