gpt4 book ai didi

batch-file - CMD 变量名限制?

转载 作者:行者123 更新时间:2023-12-04 23:22:01 24 4
gpt4 key购买 nike

批处理文件变量名有哪些限制,为什么?

我注意到我无法回显名称为 :) 的变量.

h:\uprof>set :)=123

h:\uprof>set :)
:)=123

h:\uprof>echo %:)%
%:)%

显然来自批处理文件 :)输出而不是 %:)% .问题显然不在于 set命令作为变量及其值出现在 set 的输出中.

奇怪的是,当分开时 - :) - 并反转 - ): - 当用作变量名时,所有输出它们的给定值。

最佳答案

唯一不能出现在用户定义的批处理环境变量名称中的字符是 = . SET 语句将在第一次出现 = 时终止变量名。 , 之后的所有内容都将成为值(value)的一部分。

分配一个包含 : 的变量名很简单。 ,但除非在特定情况下,否则通常无法扩展该值。

启用扩展时 (默认行为)

冒号是搜索/替换和子字符串扩展语法的一部分,它会干扰名称中包含冒号的变量的扩展。

有一个异常(exception) - 如果 :显示为名称中的最后一个字符,则变量可以很好地扩展,但是您不能对值进行搜索和替换或子字符串扩展操作。

禁用扩展时

搜索/替换和子字符串扩展不可用,因此没有什么可以阻止包含冒号的变量的扩展正常工作。

@echo off
setlocal enableExtensions

set "test:=[value of test:]"
set "test:more=[value of test:more]"
set "test"

echo(
echo With extensions enabled
echo -------------------------
echo %%test:%% = %test:%
echo %%test::test=replace%% = %test::test=replace%
echo %%test::~0,4%% = %test::~0,4%
echo %%test:more%% = %test:more%

setlocal disableExtensions
echo(
echo With extensions disabled
echo -------------------------
echo %%test:%% = %test:%
echo %%test:more%% = %test:more%

- 输出 -
test:=[value of test:]
test:more=[value of test:more]

With extensions enabled
-------------------------
%test:% = [value of test:]
%test::test=replace% = :test=replace
%test::~0,4% = :~0,4
%test:more% = more

With extensions disabled
-------------------------
%test:% = [value of test:]
%test:more% = [value of test:more]

https://stackoverflow.com/a/7970912/1012053完整描述变量扩展的工作原理。

关于batch-file - CMD 变量名限制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22158593/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com