有没有在Windows批处理脚本%之间1%1%的差异? [英] Is there a difference between %1 and %1% in windows batch scripts?
问题描述
我amtrying理解%1
和%1%
在批处理脚本,但无论哪种解释之间的差异在SO没有意义,是错误的,或者我有一个完整的误解或测试脚本无法正常工作给出。
I amtrying to understand the difference between %1
and %1%
in batch scripts, but either the explanations given in SO do not make sense, are wrong, or I have a complete misunderstanding or the test script is working incorrectly.
在Windows 7中,我使用给出的剧本<一个href=\"http://social.technet.microsoft.com/Forums/scriptcenter/en-US/6b7d6b97-3b08-4b14-8b7e-77840fb02c2f/difference-between-1-and-1-in-batch-scripting?forum=ITCG\"相对=nofollow>这里:
On Windows 7 I am using the script given here:
@echo off
set Name=Amit
set 1=First
set 2=Second
echo My own environmental variables are:
echo Name=%Name%, 1=%1%, 2=%2%"
echo The user invoked this batch file with a parameter of %1
这给下面的输出:
which give the following output:
> tester.bat
My own environmental variables are:
Name=Amit, 1=2"
The user invoked this batch file with a parameter of .
和
> tester.bat test
My own environmental variables are:
Name=Amit, 1=test2"
我的结论:%1
和%1%
是等价的。这是这样的吗?
用户调用此批处理文件与测试的参数。
My conclusion: %1
and %1%
are equivalent. Is this so?
The user invoked this batch file with a parameter of test.
由于建议的foxidrive:
As suggested by foxidrive:
@echo off
echo %1% abc %2%
pause
echo %1 abc %2
pause
运行为 tester.bat AAA BBB
给第一个输出: aaa2
和第二输出 AAA ABC BBB
。
Run as tester.bat aaa bbb
gives first output: aaa2
and second output aaa abc bbb
.
推荐答案
是%1%
是不正确的语法。
启动,像这样这个批处理文件,看看它是如何影响的脚本: file.bat AAA BBB
Launch this batch file like so to see how it can affect a script: file.bat aaa bbb
@echo off
echo %1% abc %2%
pause
echo %1 abc %2
pause
下面是控制台输出上面的:
Here is the console output for the above:
aaa2
Press any key to continue . . .
aaa abc bbb
Press any key to continue . . .
这是因为这个词%1%ABC%2%
被解析为%1
结果
然后叫环境变量%ABC%
这是不是在上述情况下的定义,结果
再加上 2
和一个孤独的%
这将被丢弃。
This happens because the term %1% abc %2%
is parsed as %1
and then an environment variable called % abc %
which is not defined in the above scenario,
plus a 2
and a lone %
which is discarded.
这篇关于有没有在Windows批处理脚本%之间1%1%的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!