![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-11 02:49 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » [Discussion] About the restrictions on the [variable=[string]] variable name |
| Printable Version 2,249 / 4 |
| Floor1 newsuper | Posted 2008-10-19 02:04 |
| 新手上路 Posts 15 Credits 18 | |
|
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. |
|
| Floor2 lxmxn | Posted 2008-10-19 05:01 |
| 版主 Posts 4,938 Credits 11,386 | |
|
When setting a value for a variable, besides the space between the set command itself and the variable name, spaces are not ignored by default. This is both an advantage and a disadvantage.
E:\>set cn dos = hey, everybody I also tested some other characters, and there doesn't seem to be any problems. For example, you can set the following I don't understand why you set the variable name like this. It's like setting a variable name as lakjsdlfjlsjfhkshfs in other languages, which is meaningless. If it's purely for researching the "features" of cmd, that's another story. |
|
| Floor3 ashu791 | Posted 2008-10-19 06:41 |
| 贫困用户 Posts 6 Credits -5 | |
|
Turn off these default shares
|
|
| Floor4 newsuper | Posted 2008-10-19 07:56 |
| 新手上路 Posts 15 Credits 18 | |
|
Re lxmxn
The reason I set a meaningless name is purely to test whether such a variable name can be set. If I set set I love my gf=, would it have a little practical meaning? Of course, the spaces can also be completely removed, just talking casually. In addition, personally, I think that the disadvantage is that the variable name after set does not ignore spaces. As for the advantages you mentioned, can you explain? Thank you. Re ashu791 I don't understand what you mean by "closing these default shares"? [ Last edited by newsuper on 2000-10-19 at 08:01 ] |
|
| Floor5 skuny | Posted 2008-12-24 11:16 |
| 初级用户 Posts 20 Credits 28 | |
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |