The Bash logo
A useful feature in almost every command-line environment is the environment variable. Some of these variables allow you to control the behavior or features of the command line, and other variables simply allow you to store data that you might need to reference later. Environment variables are also used in FreeDOS.
Variables on Linux
On Linux, you may already be familiar with several of these important environment variables. In the Bash shell on Linux, the PATH variable identifies where the shell can find programs and commands. For example, on my Linux system, I have this PATH value:
bash$ echo $PATH
/home/jhall/bin:/bin:/usr/bin:/usr/local/bin
That means when I type a command name like cat, Bash will check each of the directories listed, in order:
- /home/jhall/bin
- /bin
- /usr/bin
- /usr/local/bin
And in my case, the cat command is located in the /bin directory. (Actually, Fedora Linux merged the /bin and /usr/bin directories a while ago, so my PATH is actually an anachronism. But I keep it that way because it’s still technically correct, which is the best kind of correct.)
To set an environment variable on Linux, you type the name of the variable, then an equals sign (=) and then the value to store in the variable. To reference that value later using Bash, you type a dollar sign ($) in front of the variable name.
bash$ var=Hello
bash$ echo $var
Hello
Variables on FreeDOS
On FreeDOS, environment variables serve a similar function. Some variables control the behavior of the DOS system, and others are useful to store some temporary value.
To set an environment variable on FreeDOS, you need to use the SET keyword. FreeDOS is case insensitive, so you can type that using either uppercase or lowercase letters. Then set the variable as you might on Linux, using the variable name, an equals sign (=), and the value you want to store.
However, referencing or expanding an environment variable’s value in FreeDOS is quite different from how you do it on Linux. You can’t use the dollar sign ($) to reference a variable in FreeDOS. Instead, you need to surround the variable’s name with percent signs (%).
C:\>echo %PATH%
C:\freedos\bin
It’s important to use the percent signs both before and after the name because that’s how FreeDOS knows where the variable name begins and ends. This is very useful, as it allows you to reference a variable’s value while immediately appending (or prepending) other text to the value. Let me demonstrate this by setting a new variable called REPLY with the value yes, then referencing that value with the text “11” before and “22” after it:
C:\>set REPLY=yes
C:\>echo 11%REPLY%22
11yes22
Because FreeDOS is case insensitive you can also use uppercase or lowercase letters for the variable name, as well as the SET keyword. However, the variable’s value will use the letter case as you typed it on the command line.
Finally, you can see a list of all the environment variables currently defined in FreeDOS. Without any arguments, the SET keyword will display all variables, so you can see everything at a glance:
C:\>set
CONFIG=2
DOSDIR=C:\FreeDOS
COMSPEC=C:\FreeDOS\BIN\COMMAND.COM
PATH=C:\freedos\bin
TEMP=C:\freedos\temp
TMP=C:\freedos\temp
LANG=en
NLSPATH=C:\freedos\nls
HELPPATH=C:\freedos\help
TZ=utc
DIRCMD=/O:GNE
Environment variables are a useful staple in command-line environments, and the same applies to FreeDOS. You can set your own variables to serve your own needs, but be careful about changing some of the variables that FreeDOS uses. These can change the behavior of your running FreeDOS system:
DOSDIR– The location of the FreeDOS installation directoryCOMSPEC– The current instance of the FreeDOS shellLANG– The user’s preferred languageNLSPATH– The location of the system’s language filesTZ– The system’s time zone (not used by DOS, but some programs complain if it’s not there)PATH– A list of directories where FreeDOS can find programs to runHELPPATH– The location of the system’s documentation filesTEMP– A temporary directory where FreeDOS stores output from each command as it “pipes” data between programs on the command lineDIRCMD– A variable that controls how the DIR command displays files and directories, such as/O:GNEto order (O) the contents by grouping (G) directories first, then sorting entries by name (N) then extension (E)
If you accidentally change any of the FreeDOS “internal” variables, you could prevent some parts of FreeDOS from working properly. In that case, simply reboot your computer, and FreeDOS will reset the variables from the system defaults.
Adapted from Set and use environment variables in FreeDOS by Jim Hall