null variable in a .bat, window batch file

时间:2018-02-05 12:58:46

标签: windows batch-file

I don't find how to set a null variable in a .bat on windows.

on linux I would do:

var=""
echo $var2/${var3}_brain_mask${var}.nii.gz

and then I have as results

var2/var3_brain_mask.nii.gz

but if I do similar stuff on windows I have:

set var=""
echo %var2%/%var3%_brain_mask%var%.nii.gz

and as results

var2/var3_brain_mask"" .nii.gz

the var variable is set at the beginning of the .bat, changed by the user, and I'ld like to be able to set it as empty variable by default as I do on linux.

does anyone has an idea how to do that ?

2 个答案:

答案 0 :(得分:0)

<{1}}和Windows 8.1上的

只需将开头Windows 10放在"

之前
var

请注意!

set "var="
echo %var2%/%var3%_brain_mask%var%.nii.gz

set var=

两者都有效,但总是使用后者会更好,因为它会确保变量中没有捕获的尾随空格会导致输出出现问题

此外,如上所示,从set "var=" 开始,输出将与cmdline不同。

答案 1 :(得分:0)

在Windows 10上测试:

SET var1= & ECHO 123 %var1% abc

SET "var2=" & ECHO 123 %var2% abc

如果在ECHO 123 %var3% abc123 abc文件中,

.bat将输出.cmd

如果手动输入CMD窗口,则每个输出将为:123 %varN% abc(其中N代表123相应地)

SET var4="" & ECHO 123 %var4% abc会在123 "" abc.bat文件中输出.cmd

但是,当在CMD窗口中手动输入时,输出将为123 %var4% abc,但是:之后输入ECHO 123 %var4% abc将输出123 "" abc