Guide for Using the Terminal

Table of Contents:

Contact Benjamin Bogø if you found a problem or have suggestions for improvements/additions.

1 PATH-Variable Explained

1.1 Purpose and Contents

The PATH-variable tells where an operating system should look for executable files when running a command in a terminal. It is simply a list of absolute paths where it looks for the first match iterating through the list from the start to the end. The paths in the PATH-variable looks a bit different between Windows and Linux/MacOS.

Windows

Here are some examples of paths on Windows:

file.txt
executable.exe
C:\absolute\path\to\directory\
relative\path\to\directory\
C:\absolute\path\to\file.txt
relative\path\to\file.txt
"C:\absolute path\with spaces\"
"relative path\with spaces\"
C:\absolute\path\to\executable.exe
relative\path\to\executable.exe
Linux/MacOS

and here are the equivalent paths on Linux/MacOS:

file.txt
./executable
/absolute/path/to/directory/
relative/path/to/directory/
/absolute/path/to/file.txt
relative/path/to/file.txt
/absolute\ path/with\ spaces/
relative\ path/with\ spaces/
/absolute/path/to/executable
./relative/path/to/executable

The important differences are noted below:

  • Paths on Windows uses backslash \ while Linux/MacOS uses forward slash /. However, Windows allows usage of forward slash / in some places (commands) but might trigger errors. In addition, some programming languages needs forward slashes / on Windows, so stick to forward slash / in PATH-strings in code (if possible).
  • Absolute paths on Windows often have a drive letter (C:) while Linux/MacOS does not. External device drives may be accessible using /mnt/c/ as the drive letter.
  • Paths with spaces needs to be quoted ("") on Windows while the space characters needs to be escaped (\ ) on Linux/MacOS as space is normally used to separate arguments in commands.
  • When writing a path to an executable (file/program/script) that should be executed, then the path to the file can just be used except when it is a relative path on Linux/MacOS where it needs to be prefixed with ./.
  • Paths on Linux/MacOS might start with ~. This means that the paths starts in the current user's directory rather than the root (/).

The PATH-variable (only) contains absolute paths to directories that are separated by ; on Windows and : on Linux/MacOS. Usually, you add the paths to directories that contains executables that you use very often. Relative paths are resolved according to the so called current directory (cd). For instance, if the current directory is C:\dir1 and the relative path is dir2\dir3 then the resulting path is C:\dir1\dir2\dir3. We will look at how the PATH-variable works on Windows and Linux/MacOS below as they work in slightly different ways.

1.2 Windows

The contents of the PATH-variable on Windows can be shown with the command set PATH (or set path) and could contain the following (written in a more readable format with newlines):

PATH=
C:\dir1;
C:\path\to\dir3;
C:\dir2;
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

It is also possible to just use PATH (or path) but the last line is not included in that case. The last line with PATHEXT shows the file extensions that Windows considers executable and will look for in the directories.

It is now possible to explain what actually happens when you just write java in the terminal when the current directory is C:\dir4 and the PATH-variable above is used:

  1. Windows stops at the first file that matches the requested file.
  2. Windows will first look in the current directory (C:\dir4) for java (without extension).
  3. Windows will then look in the current directory (C:\dir4) for files named java with the extensions in PATHEXT, i.e. java.com, java.exe, jave.bat, etc.
  4. Windows will then look in the directories in the PATH-variable in order, i.e. first C:\dir1, then C:\path\to\dir3 and C:\dir2 and repeat step 2 and 3 for those directories.
  5. If no files are found it will report 'java' is not recognized as an internal or external command, operable program or batch file..

You can use where java to get the (ordered) list of files matching the rules above.

1.3 Linux/MacOS

The contents of the PATH-variable on Linux can be shown with the command echo $PATH and could contain the following (written in a more readable format with newlines):

/dir1:
/path/to/dir3:
/dir2

It is now possible to explain what actually happens when you just write java in the terminal when the current directory is /dir4 and the PATH-variable above is used:

  1. Linux/MacOS stops at the first file that matches the requested file.
  2. Linux/MacOS will NOT look in the current directory (/dir4) for java (without extension) unless you write ./java!
  3. Linux/MacOS will look in the directories in the PATH-variable in order, i.e. first /dir1, then /path/to/dir3 and /dir2 and then look for exactly the file java.
  4. If no files are found it will report 'java' is not recognized as an internal or external command, operable program or batch file..

Note that executables on Linux do not (normally) have file extensions i.e. it is just java rather than java.exe or java.dmg. You can use which -a java to get the (ordered) list of files matching the rules above.

2 [Windows] Change PATH-variable

2.1 Open Environment Variables

In Windows 10 Search (Contra) ( + ), search for Environment Variables (in your language, i.e. Danish: miljøvariabler) and choose Edit System Environment Variables. In the dialog, choose Environment Variables... near the bottom.

2.2 Choose Variable

In the next dialog, there are two boxes. The top one is your user variables and the bottom one is the system variables (applies to all users). Select Path in the box where you want to add a path (in case you are the only user, then it does not matter which one). Click on the button below the chosen box.

2.3 Change Paths

In the next dialog, the lines on the left shows existing paths. To add a new path, click on and paste the path you want to add (or use ). Select a path and use to change it. Select a path and use to remove it.

When you are done, make sure to click to all dialogs. For the changes to take effect, restart the terminal (or write cmd to start a new terminal in the current).

2.4 (OPTIONAL) Alternatives

You can always use the direct path with quotes to the executable instead of adding it to your PATH-variable. In case you want to run java, you can use (.exe can be left out):

"C:\path\to\bin\java.exe" --version

You can also modify the PATH-variable for the current terminal session only (see HELP PATH command for more):

PATH C:\path\to\bin\java.exe;%PATH%
java --version

These approaches are recommended if you do not expect to use it later (likely not the case for java).

3 [Linux] Change PATH-variable

3.1 Change PATH-variable Command

This depends on the Linux distribution you use. However, all cases requires editing a special file. Search for the path online and modify the file. On Ubuntu, it is /etc/profile (all users) and/or ~/.profile (current user only). Remember to restart the terminal when you have performed the changes.

3.2 (OPTIONAL) Alternatives

You can always use the direct path to the executable instead of adding it to your PATH-variable. In case you want to run java, you can use:

/path/to/bin/java --version

You can also modify the PATH-variable for the current terminal session only (see HELP PATH command for more):

PATH=/path/to/bin/java:$PATH
java --version

These approaches are recommended if you do not expect to use it later (likely not the case for java).

4 [MacOS] Change PATH-variable

4.1 Change /etc/paths

You just need to modify the file /etc/paths (edit with text editor - remember separating the paths with :). Note that it will change the PATH-variable for ALL users (does not matter if you are the only user). Remember to restart the terminal when you have performed the changes.

Alternatively, look for other paths online.

4.2 (OPTIONAL) Alternatives

You can always use the direct path to the executable instead of adding it to your PATH-variable. In case you want to run java, you can use:

/path/to/bin/java --version

You can also modify the PATH-variable for the current terminal session only (see HELP PATH command for more):

PATH=/path/to/bin/java:$PATH
java --version

These approaches are recommended if you do not expect to use it later (likely not the case for java).

5 Common Problems

5.1 Command Not Found

If you get something like:

Windows:

'java' is not recognized as an internal or external command, operable program or batch file.

Linux:

java: command not found

Then restart you terminal or check your PATH-variable.

5.2 Wrong Version

In case you get the wrong version (likely for java --version) then run the following command as it will tell you if there are multiple programs that matches the pattern.

Input: Name of command (program/executable) to search for.
Windows
where java
Linux
which -a java
MacOS
which -a java

Remember that the first one is chosen, so you might want to modify your PATH-variable: