Whenever we open up a command line window, it starts us at our home directory as a default. On my computer, it is c:\documents and settings\rich>. On your computer, it is probably something completely different (unless if your mother gave your the awesome name Rich too, of course!) Not only there are defaults, defaults can be changed. For example, your ‘documents and settings’ folder might not be on c:. Maybe it is on d:. Maybe the folders under that are at different locations. For example, on my computer, I only care about my documents folder. Therefore, I moved it to a separate partition, so when I ever have to reinstall Windows, I can do so in such a way so that my documents folder is untouched. However, programs that I use still have no trouble at all in finding the my documents folder.
How does this happen? Surely, there must be something that is holding the path to our home or documents folders. Well, there is! Based on the name of this post, you can probably guess what’s doing it, too. There are variables that are holding this information. A variable is something that is used to reference an item when the item is unknown. A variable, in computers, can be a number, a letter, a string, a path, or an object such as a program or a command. It could be anything. If you are or planning on doing anything with batch files, script files or programming, you will be working with variables.
An environment variable is a variable that is used as a parameter for a specific computer. For our purposes, these are paths. On my computer, my home directory may be different than on yours because my environment variable is different. Now, I said in the previous paragraph that a variable is used when the item is unknown. But, isn’t it? How does the command line know where to go if it is not known? That is because the command line referenced the variable name instead of the path name. The reason why is because the command line does not know the path name, only the variable name. Then, the variable name gets translated to the path.
In Windows (and the command line), a variable is distinguished by characters or words surrounded by percent (%) signs. So, say that there is a variable named ‘home’, the variable would be %home%. These variables are set in the registry. The registry is then read every time a command window is opened, and then the variables get set. We will not be working with the registry since one wrong move and your system will not boot. However, some of the variables are very easy to change in the GUI without using the registry. For example, the documents directory can be changed by just right-clicking your documents folder, select ‘properties’ and click on the ‘move’ button. Most everything else can be changed by right-clicking your computer icon, go to ‘properties’, and select the ‘advanced’ tab. However, I would not recommend you changing them unless you know what you are doing.
To see how variables work, I will show you an easy way to bring up your temp folder. Go to start->run, type %tmp% and press enter. This will bring up the folder that is used by programs to store temporary files (usually during installation.) Ok, now bring up a command window (start->run, type cmd). We should be now at the familiar home directory. How do we know what environment variable the command window uses to get to there? We can see all the variables that are being used by the set command. The set command displays variables being used and sets variables (creates them). Type in set and press enter.
The list is just a bit bigger than what can fit on the screen. However, you can use the scroll to move back up to the top of the list. Do you see a variable named ‘homepath’? That is the environment variable the command window uses in order to go to your home directory. This can be displayed by typing in echo %homepath% and press enter.
NOTE: I stated this in a previous post, but Windows is not a case sensitive system. This applies to path names and variables. On most other platforms (and programming languages), they are case sensitive. So, Myvariable, myvariable and MYVARIABLE are different variables on most systems, but in Windows, they are the same.
Now that you know what variables are, let’s have some fun! We are going to assign a couple of variables and do some simple arithmetic. Type in set num1=2 and press enter. Now, type in set and see if the variable is actually assigned (should see num1=2). Now, type in set num2=3 and press enter. You can see what is inside the variables using the echo command. Type in echo %num2% and press enter. You should see ’3′ on the next line.
If you noticed, we did not use the percent (%) signs when we assign a variable. This is correct syntax. You have to distinguish a variable when calling it, not assigning it. This is true, even with the set command. Confusing? That is why I am going to have you do the next sample.
Type in set num3=%num1%+%num2% and press enter. We are setting the ‘num3′ variable, so we don’t need percent (%) signs. However, variables ‘num1′ and ‘num2′ are already assigned. So, to call them, you have to use the percent (%) signs.
Type in echo %num3% and press enter. Is it what you expect? Shouldn’t it say ’5′ instead of ’2+3′? Well, there is one thing that does look right. The %num1% was replaced by 2 and %num2% was replaced by 3. But, why didn’t it add them? That is because we have to specify the arithmetic flag (/a). Type set /a num3=%num1%+%num2% and press enter. The next line should say ’5′. But, to confirm that was assigned to the variable ‘num3′, type echo %num3% and press enter. It should now say ’5′ instead of ’2+3′.
You might have noticed, but when reading all of the environment variables, there is one variable named ‘prompt’. This is the prompt of your command line. Currently, it displays the path to where you are at, followed by the greater than (>) sign. You can change this! However, instead of using the set command, we will use the prompt command. The prompt command is used to set the prompt of your command line window. In order to see the different things you can do with it, type prompt /? and press enter. Here, you can see the different items you can display on your prompt.
Let’s do something interesting. Not only we can have the current directory we are working in, we can also have the date and time as well. Type prompt $D $S $T $S $P $G and press enter. You should now see the date, followed by the time, followed by the current path, followed by a greater than sign. Wait a few seconds and press enter. You can see the time change to the new current time.
Have fun with the variables and prompt. I would not recommend changing any of the important system variables (like windows, programs or homepath.) A great thing (or bad thing) is that the environment variables you change (including the prompt) is only valid in the current window you are working in. If you close out the window and reopen a new command window, all of the variables will reset to the defaults.
Join the forum discussion on this post
