This is an update of a post that originally appeared on February 24, 2014.
A short while ago, I created a post about the Windows path. A number of people wrote me about that post with questions. Yes, you can use the technique for setting the
A few of you took me to task for not mentioning the
Set command. This particular command appears in Microsoft Windows Command Line Administration Instant Reference. It’s a useful command because you can temporarily configure a command prompt session to support a new set of settings. When the session is ended, the settings are gone. Only those settings you create as part of Environment Variables window have any permanence. There are other tricks you can use, but using
Set for temporary environment variables and the Environment Variables window for permanent environment variables are the two most common approaches.
In order to see the current environment variables you simply type Set and press Enter at the command line. If you add a space and one or more letters, you see just the matching environment variables. For example, type Set U and press Enter to see all of the environment variables that begin with the letter U.
To set an environment variable, you add the name of the variable, an equals sign (=), and the variable value. For example, to set the value of
Hello, you type Set MyVariable=Hello and press Enter. To verify that
MyVariable does indeed equal
Hello, you type Set MyVariable and press Enter. The command prompt will display the value of
MyVariable. When you’re done using
MyVariable, you can type Set MyVariable= and press Enter. Notice the addition of the equals sign. If you ask for the value of
MyVariable again, the command prompt will tell you it doesn’t exist.
Newer versions of the command prompt provide some additional functionality. For example, you might set
MyVariable within a batch file and not know what value it should contain when you create the batch file. In this case, you can prompt the user to provide a value using the
/P command line switch. For example, if you type Set /P MyVariable=Type a value for my variable: and press Enter, you’ll see a prompt to enter the variable value.
It’s also possible to perform math with Set using the
/A command line switch. There is a whole list of standard math notations you can use. Type Set /? and press Enter to see them all. If you write application code at all, you’ll recognize the standard symbols. For example, if you want to increment the value of a variable each time something happens, you can use the
+= operator. Type Set /A MyVariable+=1 and press Enter to see how this works. The first time you make the call,
MyVariable will equal
1. However, on each succeeding call, the value will increment by
1 (for values of
3, and so on).
Environment variables support expansion and you can see this work using the
Echo command. For example, if you type Echo %MyVariable%, you see the value of
However, you might not want the entire value of
MyVariable. Newer versions of the command prompt support substrings. The variable name is followed by a
:~, the beginning position, a comma, and the ending position. For example, if you place
Hello World in
MyVariable, and then type Echo %MyVariable:~0,5% and press Enter, you see
Hello as the output, not
Hello World. Adding a negative sign causes the expansion to occur from the end of the string. For example, if you type Echo %MyVariable:~-5% and press Enter, you see
World as the output.
Set command is a valuable addition to both the administrator’s and programmer’s toolkit because it lets you set environment variables temporarily. The
Set command figures prominently in batch file processing and also provides configuration options for specific needs. Let me know about your environment variable questions as they pertain to my books at [email protected].