set variable with equal sign in its name

Microsoft Windows
Post Reply
User avatar
MigrationUser
Posts: 336
Joined: 2021-Jul-12, 1:37 pm
Contact:

set variable with equal sign in its name

Post by MigrationUser »

01 May 2013 20:40
npocmaka

First you need a batch file like this:

Code: Select all

@echo off
rem -- do not add anything at the 
rem    end of the file
rem    After second ^ is the EOF
set liberte=egalite=fraternite=^^
Now... If you can try echo %liberte=egalite=fraternite% , but at the moment it's value is ^ .But you can change it's value by using SET
e.g. SET liberte=egalite=fraternite=ou la mort

This works also from batch file , but eventually with one additional bat that will contain the set line.
I had no success with setting the equal sign in the beginning so this cannot be achieved this way.

Last edited by npocmaka (02 May 2013 12:14)

----------------------------

#2 01 May 2013 22:55
dbenham


An environment variable name accessed by CMD simply cannot contain = sign, nor can it contain a null character (0x00). All other characters are valid for variable names.

Dave Benham

----------------------------

#3 02 May 2013 06:55
npocmaka
dbenham wrote:

An environment variable name accessed by CMD simply cannot contain = sign, nor can it contain a null character (0x00). All other characters are valid for variable names.

Dave Benham
What OS have you used?

Here's the test on my Vista machine (with the bat from the first post which I called equality):

Code: Select all

C:\Users\tmp>equality.bat

C:\Users\tmp>echo %liberte=egalite=fraternite%
More?
More?
ECHO is on.

C:\Users\tmp>set liberte=egalite=fraternite=ou la mort

C:\Users\tmp>echo %liberte=egalite=fraternite%
ou la mort
So I've made the same test on XP also , but the result was different:

Code: Select all

c:\> echo %liberte%
More?
More?
egalite=fraternite

if I run a .bat like this on both machines the behavior is again different:

Code: Select all

rem -- EOF after ^
set a=^
On Vista I should pres ctrl+break or ctrl+c to stop it's execution and ctrl+c was executed before the set command
On XP it simply says that the command is too long.

In conclusion it can be done on Vista but not on XP.
At the moment I'm too far from my Win7 machine (or it's too far from me) and I can't test it there.So I'll be glad if someone else check this...

Last edited by npocmaka (02 May 2013 08:33)

----------------------------

#4 02 May 2013 10:14
jeb


Wow :clap: very nice, but not the way you thought. :)

Code: Select all

set liberte=egalite=fraternite=ou la mor
echo %liberte=egalite=fraternite%
echo %liberte%
Output:

Code: Select all

ou la mor
egalite=fraternite=ou la mor
It sets the variable liberte not liberte=egalite=fraternite.

BUT, it seems that liberte=egalite=fraternite can be expanded and also seems to be defined.

The IF defined works only reliable with delayed expansion.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set one=two=three
echo #1 %one%
echo #2 %one=two%
echo #3 %one=other%
if defined one echo #4 one is defined
set "var1=one=two"
set "var2=one=other"
if defined one^=two echo #5 one=two is defined, normal test
if defined one^=other echo #6 one=other is defined, normal test
if defined !var1! echo #7 !var1! is defined, delayed test
if defined !var2! (echo #8 !var2! is defined, delayed test ) ELSE echo #8 !var2! is NOT defined, delayed test

#1 two=three
#2 three
#3
#4 one is defined
#5 one=two is defined, normal test
#6 one=other is defined, normal test
#7 one=two is defined, delayed test
#8 one=other is NOT defined, delayed test
jeb

Last edited by jeb (02 May 2013 10:14)

----------------------------

#5 05 May 2013 12:19
npocmaka


Thanks.The SET is tricky...
Post Reply