Because I'm a beginner, I'm not very clear about the restrictions on the variable names in SET [variable=[string]]. After checking the help with set /?, I found this sentence: "The SET command does not allow variable names to contain equal signs."
At first, I didn't pay attention to this sentence. When doing exercises, I found a problem. I tested in the command-line interpreter:
1. C:\Documents and Settings\wang>set var =look
2. C:\Documents and Settings\wang>set name=%var:l=b%
3. C:\Documents and Settings\wang>set name
4. name=%var:l=b%
I was very puzzled why name wasn't replaced by var. Then I used set var and found:
5. C:\Documents and Settings\wang>set var
6. var =look
At this time, I felt a little problem (because I didn't find the problem yet). Then I used echo %var% and found:
7. C:\Documents and Settings\wang>echo %var%
8. %var%
Suddenly, I felt that var was problematic. Then I used echo %var %:
9. C:\Documents and Settings\wang>echo %var %
10. look
At this time, I suddenly understood that when I set var =look in the first sentence, it was setting var (with a space), not var. So after the second sentence name=%var:l=b%, the value of name was =%var:l=b%. My original intention was to replace look with book and assign it to name.
In the 5th and 6th sentences, the reason why I could use set var to see the value of var was because of another help sentence of SET:
You can use only one variable to activate the SET command. If there is no equal sign or value, all prefix-matched variable values of the SET command will be displayed.
For example:
SET P
will display all variables starting with the letter P
In this way, because I didn't pay attention to the space problem before, I didn't pay much attention here.
It wasn't until the 7th and 8th sentences that I felt there might be a space problem. Finally, I found that it was indeed this problem.
Because when I used other languages to write programs before, I always used to add a space after the variable name, which was convenient for reading. But in DOS's set, a variable with a space and one without a space are two different variables.
After finding this problem, I thought of the help sentence at the beginning. Then can other special symbols be used in variable names except =?
I simply tested and found the following problems:
1. Special characters like & cannot be used in variable names (for example, set po6&9$%^155 =123). If used, it will be considered as the connection of two commands and an error will be reported (this is just my test, I don't know about others).
2. Paired quotation marks "" cannot be used in variable names (for example, set hha"#%45" rg =456). If used, an error will also occur.
3. If there is ^ in the variable name, no error will be reported, but ^ will be filtered out.
I also tested some other characters, and there seemed to be no problems. For example, I can set:
set [{!}~e ge w4%t %] sd =6
In this way, the value of the variable [{!}~e ge w4%t %] sd is 6.
In this way, it's not that only = cannot be used in variable names, and variables ending with spaces are easily confused with non-space-ending variables of the same name by people.
The above are just some problems I found when doing exercises. I hope experts can give theoretical explanations. Thank you.
At first, I didn't pay attention to this sentence. When doing exercises, I found a problem. I tested in the command-line interpreter:
1. C:\Documents and Settings\wang>set var =look
2. C:\Documents and Settings\wang>set name=%var:l=b%
3. C:\Documents and Settings\wang>set name
4. name=%var:l=b%
I was very puzzled why name wasn't replaced by var. Then I used set var and found:
5. C:\Documents and Settings\wang>set var
6. var =look
At this time, I felt a little problem (because I didn't find the problem yet). Then I used echo %var% and found:
7. C:\Documents and Settings\wang>echo %var%
8. %var%
Suddenly, I felt that var was problematic. Then I used echo %var %:
9. C:\Documents and Settings\wang>echo %var %
10. look
At this time, I suddenly understood that when I set var =look in the first sentence, it was setting var (with a space), not var. So after the second sentence name=%var:l=b%, the value of name was =%var:l=b%. My original intention was to replace look with book and assign it to name.
In the 5th and 6th sentences, the reason why I could use set var to see the value of var was because of another help sentence of SET:
You can use only one variable to activate the SET command. If there is no equal sign or value, all prefix-matched variable values of the SET command will be displayed.
For example:
SET P
will display all variables starting with the letter P
In this way, because I didn't pay attention to the space problem before, I didn't pay much attention here.
It wasn't until the 7th and 8th sentences that I felt there might be a space problem. Finally, I found that it was indeed this problem.
Because when I used other languages to write programs before, I always used to add a space after the variable name, which was convenient for reading. But in DOS's set, a variable with a space and one without a space are two different variables.
After finding this problem, I thought of the help sentence at the beginning. Then can other special symbols be used in variable names except =?
I simply tested and found the following problems:
1. Special characters like & cannot be used in variable names (for example, set po6&9$%^155 =123). If used, it will be considered as the connection of two commands and an error will be reported (this is just my test, I don't know about others).
2. Paired quotation marks "" cannot be used in variable names (for example, set hha"#%45" rg =456). If used, an error will also occur.
3. If there is ^ in the variable name, no error will be reported, but ^ will be filtered out.
I also tested some other characters, and there seemed to be no problems. For example, I can set:
set [{!}~e ge w4%t %] sd =6
In this way, the value of the variable [{!}~e ge w4%t %] sd is 6.
In this way, it's not that only = cannot be used in variable names, and variables ending with spaces are easily confused with non-space-ending variables of the same name by people.
The above are just some problems I found when doing exercises. I hope experts can give theoretical explanations. Thank you.
