- What are PATH and other environment variables, and how can I set or use them?
- 5 Answers 5
- What are Environment Variables?
- Path
- Other
- Creating new environment variables
- Windows
- GUI
- Command Line
- Format
- echo
- set
- setx
- List of Windows Environment Variables
- Unix derivatives (FreeBSD, GNU / Linux, OS X)
- What is this $PATH in Linux and how to modify it
What are PATH and other environment variables, and how can I set or use them?
are very common here, and in most cases the answers are very similar to each other. In the future it would be nice to have a good Q/A for this.
So the question is: What are environment variables, like the executable PATH , and how can I change and use them on major operating systems?
A good answer would include a simple explanation of what environment variables and especially PATH mean to the OS, as well as simple guidelines on how to set and read them accordingly.
Tired of having to do it manually each time I needed to, I wrote some registry entries to add context menu entries for AddToPath and RemoveFromPath to folders, utilizing a free app called pathed.exe . Following is the link to the complete guide: addictivetips.com/windows-tips/… I can now very easily add folders to PATH variable or remove them from it, without having to edit any file or worry about messing up the formatting of the variable. Hope it helps. =)
On Windows there is a shortcut for opening properties of System ie. Control Panel -> System It’s WIN key on keyboard + Pause/Break (WIN+Break). This is extremely helpful and speeding up process of setting new environmental variables on Win8 because there you have to click through several windows in modern ui (Yeah. If it’s modern then I’ll grow a cactus on my palm) which is just annoying. Of course you can create powershell script or use setx command and don’t worry any more about it 😀
In Windows 8, the fastest navigation to changing system and user environment variables is using search. Win+W brings up the search for all settings. Search for env and the needed options are listed.
I think it’s faster and clearer by opening CMD and typing set , or PowerShell and typing Get-Childitem env: .
If you’re editing the path variable a lot (like when setting up a new system), it might help to have a shortcut to the System Properties dialog on the desktop. To do so, right click on the desktop, select New Shortcut, and enter systempropertiesadvanced.exe. Then you can click on the link to get to the System Properties dialog, then click on Environment Variables to get to the dialog with the path settings.
5 Answers 5
What are Environment Variables?
Environment variables hold values related to the current environment, like the Operating System or user sessions.
Path
One of the most well-known is called PATH on Windows, Linux and Mac OS X. It specifies the directories in which executable programs* are located on the machine that can be started without knowing and typing the whole path to the file on the command line. (Or in Windows, the Run dialog in the Start Menu or + R ).
On Linux and Mac OS X, it usually holds all bin and sbin directories relevant for the current user. On Windows, it contains at least the C:\Windows and C:\Windows\system32 directories — that’s why you can run calc.exe or notepad.exe from the command line or Run dialog, but not firefox.exe . (Firefox is located in C:\Program Files\Mozilla Firefox . For information on how to include Firefox, go here.)
For example, typing calc (the .exe can be omitted) in the command line on Windows will start up the Windows Calculator.
* You can add support for file extensions other than .exe by editing %PATHEXT% .
Other
Other variables might tell programs what kind of terminal is used ( TERM on Linux/Mac OS X), or, on Windows, where the Windows folder is located (e.g., %WINDIR% is C:\Windows ).
Creating new environment variables
In Windows, Linux and Unix, it’s possible to create new environment variables, whose values are then made available to all programs upon launch.
You can use this when writing scripts or programs that are installed or deployed to multiple machines and need to reference values that are specific to these machines. While a similar effect can be achieved using program-specific configuration settings, it’s easier to do this using an environment variable if multiple programs need to access the same value.
Windows
GUI
- Open Control Panel » System » Advanced » Environment Variables .
- Type control sysdm.cpl,,3 in the Run dialog ( + R ) and click Environment Variables .
For editing user variables you can also type
%windir%\System32\rundll32.exe sysdm.cpl,EditEnvironmentVariables
in the Run dialog.
- In XP click on Advanced » Environment Variables .
- In Vista+ click on Advanced system settings » Environment Variables .
Environment variables in Windows are separated into user and machine/system specific values. You can view and edit their values there. Their current values upon launch are made available to all programs.
There is also Rapid Environment Editor, which helps setting and changing environment variables in Windows without the need to go deep into the system settings. Another open source program for Windows with which the path environment can be edited very conveniently is Path Editor.
Command Line
Format
Environment Variables in Windows are denoted with percent signs (%) surrounding the name:
echo
To display an environment variable’s value in cmd.exe , type echo %name% .
C:\>echo %USERPROFILE% C:\Users\Daniel
set
To create/set a variable, use set varname=value :
C:\>set FunnyCatPictures=C:\Users\Daniel\Pictures\Funny Cat Pictures C:\>set FunnyCatPicturesTwo=%USERPROFILE%\Pictures\Funny Cat Pictures 2
To append/add a variable, use set varname=value;%varname% :
C:\>set Penguins=C:\Linux C:\>set Penguins=C:\Windows;%Penguins% C:\>echo %Penguins% C:\Windows;C:\Linux
Environment variables set in this way are available for (the rest of) the duration of the Command Prompt process in which they are set, and are available to processes that are started after the variables were set.
setx
To create/set a variable permanently, use setx varname «value» :
C:\>setx FunnyCatPictures "C:\Users\Daniel\Pictures\Funny Cat Pictures" [Restart CMD] C:\>echo %FunnyCatPictures% C:\Users\Daniel\Pictures\Funny Cat Pictures
Unlike set , there is no equals sign and the value should be enclosed in quotes if it contains any spaces. Note that variables may expand to a string with spaces (e.g., %PATH% becomes C:\Program Files ), so it is best to include quotes around values that contain any variables.
You must manually add setx to versions of Windows earlier than Vista.
Windows XP Service Pack 2 Support Tools
List of Windows Environment Variables
Here is a list of default environment variables, which are built into Windows. Some examples are: %WINDIR% , %SystemRoot% , %USERPROFILE% , and %APPDATA% . Like most names in Windows, these are case-insensitive.
Unix derivatives (FreeBSD, GNU / Linux, OS X)
Environment Variables in Linux are prefixed with a dollar sign ($) such as $HOME or $HOSTNAME. Many well-known and standard variables are spelled out in capital letters to signify just that. Keep in mind that variable names are case-sensitive, meaning that $User and $USER are entirely unrelated from the shell’s point of view.
Unix derivatives define system wide variables in shell scripts located mostly in the /etc folder, but user-specific values may be given to those variables in scripts located in the home folder (e.g., /etc/profile , $HOME/.bash_profile ). The .profile file in the home folder is a common place to define user variables.
Setting variables
These files are regular shell scripts and can contain more than just environment variable declarations. To set an environment variable, use export . To show your currently defined environment variables in a terminal, run env .
The export command is a standard way to define variables. The syntax is very intuitive. The outcome is identical for these two lines, but the first alternative is preferable in case portability to pre-POSIX Bourne shell is necessary.
var=value; export var export var=value
The C shell and its descendants use a completely different syntax; there, the command is setenv .
See the Linux documentation project, Path HOWTO for a more thorough discussion on this topic.
Perhaps contrary to common belief, OS X is more «Unix» than Linux. Additionally to the files already mentioned, $PATH can be modified in these files:
- /etc/paths contains all default directories that are added to the path, like /bin and /usr/sbin .
- Any file in /etc/paths.d — commonly used by installers to make the executable files they provide available from the shell without touching system-wide or user-specific configuration files. These files simply contain one path per line. e.g., /Programs/Mozilla/Calendar/bin.
What is this $PATH in Linux and how to modify it
Yes, you can change it — for example add to the $PATH folder with your custom scripts.
So: if your scripts are in /usr/local/myscripts to execute them you will have to type in a full path to the script: /usr/local/myscripts/myscript.sh After changing your $PATH variable you can just type in myscript.sh to execute script.
Here is an example of $PATH from RHEL:
To change your $PATH you have to either edit ~/.profile (or ~/.bash_profile ) for user or global $PATH setting in /etc/profile .
One of the consequences of having inaccurate $PATH variables is that shell will not be able to find and execute programs without a full $PATH .
Oh my God, you clarified it all for me by your statement —> «So: if your scripts are in /usr/local/myscripts to execute them you will have to type in a full path to the script: /usr/local/myscripts/myscript.sh After changing your $PATH variable you can just type in myscript.sh to execute script.» Thanks a lot
@ruggedbuteducated just bash commands which are executed after you log in. Please look into man bash and search for bashrc.
Firstly, you are correct in your statement of what $PATH does. If you were to break it somehow (as per your third point), you will have to manually type in /usr/bin/xyz if you want to run a program in /usr/bin from the terminal. Depending on how individual programs work, this might break some programs that invoke other ones, as they will expect to just be able to run ls or something.
So if you were to play around with $PATH, I would suggest saving it somewhere first. Use the command line instruction
echo $PATH > someRandomFile.txt
to save it in someRandomFile.txt
You can change $PATH using the export command. So
HOWEVER, this will completely replace $PATH with someNewPath. Since items in path are separated by a «:», you can add items to it (best not to remove, see above) by executing
The fact that it is an environmental variable means that programs can find out its value, ie it is something that is set about the environment that the program is running in. Other environmental variables include things like the current directory and the address of the current proxy.