CMD variable name restrictions?

What are the restrictions on batch variable names and why?

I noticed that I can not repeat the variable with the name :).

h:\uprof>set :)=123

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

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

From the batch file, it is obviously :)output instead %:)%. The problem is clearly not related to the command set, since the variable and its value appear on the output set.

It is strange when separated - :or )- and vice versa - ):- all give out their given values ​​when used as variable names.

+4
source share
3 answers

:- a special string manipulation character to expand variables. Example:

%var:~0,1%

, - : , . : .

: :, , .

. set /?


set :)=123
set a)=123
set :a=123
set :=123
set )=123
echo %:)%
echo %a)%
echo %:a%
echo %:%
echo %)%

:

%:)%
123
%:a%
123
123
+4

, , , =. SET =, .

, :, , .

( )

/ , , .

: : , , .

/ , , , , .

@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]

. fooobar.com/questions/4522/... , .

+4

, .

, , , set "1var=foo", , %1 %1var%, , , var%. , ~ parser, , , %~dp0. , (set "~dp=bar"), %~dp%, :

The following usage of the path operator in batch-parameter substitution is invalid: %~dp%

Access to such variables is still possible with the help of a delayed extension ( !1var!, !~dp!), but for clarity, it is better not to start the variable names with a digit or tilde.

0
source

Source: https://habr.com/ru/post/1529897/


All Articles