- What is the PATH environment variable and how do I add to it?
- 4 Answers 4
- Method 1
- Method 2
- List all variables on terminal
- Show one variable at a time
- Add or change a variable
- Show PATH of Environment Variables
- Environment Variables
- PATH
- PRINTENV
- ECHO
- Modifying PATH
- How to Show PATH of Environment Variables in Linux?
- Method 1: Using the “echo” Command
- Method 2: Using the “cat” Command
- Optional: Update the PATH Environment Variable
- Method 3: Using the “env” Command
- Method 4: Using the “set” Command
- Method 5: Using the “printenv” Command
- Conclusion
What is the PATH environment variable and how do I add to it?
PATH is a global operating system variable that contains names of files that are to be executed without specyfing the whole path to them. For example You can just write startx to start graphic environemnt instead of /bin/some other folders/startx
After editing .profile restart your system to get the changes to take effect. (Perhaps there is a way around this, but restarting certainly works)
4 Answers 4
PATH is an enviroment variable. It basically tells your machine where to search for programs, so when you run your picc program you can just do this:
To add a directory to your $PATH , follow either one of the options below.
Method 1
PATH="$HOME/bin:$PATH:/usr/hitech/picc/9.82/bin"
Method 2
Run the command below in the terminal:
export PATH=$PATH:/usr/hitech/picc/9.82/bin
i do not clearly understand what do i edited /etc/profile but the link in your answer says something like this if [ -d «$HOME/bin» ] ; then PATH=»$PATH:$HOME/bin» fi what do do with this?
Just keep adding a colon, then your new path to that string. i.e. PATH=»$HOME/bin:$PATH:/usr/hitech/picc/9.82/bin:/my/new/path»
Shell environment variables are used for several purposes, from storing data, storing software configurations, set terminal settings, and changing shell environment. The environment variables are normally set at boot time, or by different software as required. One way of setting environmental variables is from the command line.
List all variables on terminal
this will print all the variable that you have
Show one variable at a time
The amount of these variables can become a very long list and locating one specific variable can become a tough task. Fortunately Linux allows us to display the value of one shell variable by using the echo command along with the name of the variable. This makes the task very easy. example: echo «$HOME»
Add or change a variable
To add or modify an environment variable, we can use the export command followed by the name of the variable and the values that go with it.
export NameofVariable='value'
Note, however, that this will only work for the current shell session. It won’t be available in any other terminals.
Show PATH of Environment Variables
An environment variable is a key:value pair in the form of key=value. The value itself can be a number, a location, text, or any other random string. The environment variables shape the system and define various attributes. For example, the HOME variable is used to set the current user’s home folder, while the SHELL variable contains information about the current user’s shell path. Although there are a large number of pre-defined environment variables, one can always create new ones or modify existing ones. This means that any environment variable can be shown/seen, edited, saved, and deleted. And though environment variables exist on every system, we will be focusing on the Linux system. In particular, in this tutorial, we will be learning about showing the PATH of the environment variables.
Environment Variables
In order to print out all of the environment variables, we type:
The latter will print out all the environment variables and their respective values.
The printenv command is used to print out each and every environment variable. That is the key:value pairs. Some of the environment variables on my system are:
SHELL=/bin/bash |
PWD=/home/kalyani |
HOME=/home/kalyani |
TERM=xterm-256color |
USER=kalyani |
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin |
The first thing to notice is a key:value pair. Of all these environment variables, one that we give specific attention to is the PATH environment variable.
PATH
PATH is an environment variable that allows the system to specifically find various executables by pointing them to the right directory. On Ubuntu, PATH would look something like this (this is on my system):
PATH = / usr / local / sbin: / usr / local / bin: / usr / sbin: / usr / bin: / sbin: / bin: / usr / games: /
usr / local / games: / snap / bin
PATH here is the key, and to the right of the PATH variable is its value. By convention, each path is separated by a colon (:). And separated by a colon are a large number of directories.
So when you enter a command, let’s take the command “cat” as an example; the shell will search each one of these directories one after another from left to right. If it is found in one of them, then it executes it. However, please note that it will search in the exact order that it is listed in. This means it won’t search /snap/bin first; it will search it last. Further, when it has searched the /usr/local/sbin directory, it won’t randomly pick another one to search but rather search the /usr/local/bin directory.
We notice that the cat command is found in /usr/bin, and as such when the system gets to /usr/bin, it will find the cat command, execute it, and terminate the search. This further means that if there were two versions of the “cat” command, the one that appears in the earlier directory would be executed, and the second one would be ignored. If the command is not found in any of the directories listed in the PATH environment variable, then a “command not found” error will be issued.
On a Linux system, there are a number of ways of displaying the PATH information.
PRINTENV
We can obviously display the path information using the printenv command. In order to print out the PATH information using the printenv command, you need to type printenv and then the name of the environment variable that you would like to display. In our case, it’d be PATH.
You can add any of the environment variables of your choice after the printenv command, and it will print it out for you.
Please note here that the word PATH has to be in all-capitals. Suppose that I write the following:
The latter returns absolutely nothing. This means no value is associated with the variable path (in lower case letters).
ECHO
Alternatively, the other common way of printing out the PATH environment variable is by using the echo command. The echo command is used to print out or echo out the value. Because PATH is a variable, in order to print it, we must add a dollar sign in front of it. Please also remember to keep all letters in the capital.
You can show the path individually using:
SED is used in this case to re-format the output by taking each path and placing them on a single line. In other words, substitute the colon with a new line.
Further, if we were to write the word PATH in lower case letters, we’d get the following:
What this means is that everything here is case-sensitive.
Modifying PATH
The PATH variable can easily be modified as well. We can add to it – prepend or append. What’s more? We can make temporary changes or permanent changes.
There are 3 ways to modify PATH:
Temporarily:
export PATH = $PATH : / path / to / dir
export PATH = / path / to / dir: $PATH
To temporarily change the PATH, you can simply type the previous code into a terminal. However, this will be restricted to a single session.
Permanently (for the current user):
Edit the .bashrc file by adding export PATH=$PATH:/path/to/dir to it, and updating the the .bashrc file.
System-wide (for all users):
Modify the /etc/environment file. This is not recommended.
Once you have modified the PATH variable, you can again check the new PATH variable using the printenv command or the echo command.
So let’s try the temporary PATH change. Suppose that I type the following into my terminal:
export PATH = $PATH : / home / kalyani
Please notice how the PATH is added to the end of the search directories.
Environment variables are a set of key:value pairs. They exist on every system, and on the Linux system, they can be viewed, edited, saved, and deleted. PATH, in particular, is an environment variable used to display the path to search for executables. When a command is issued, the system looks at the PATH environment variable to find its location. If found, the command works; otherwise, a “command not found” error is issued. Further, the PATH environment variable can be viewed using two distinct commands – the printenv command or the echo command. The PATH variable can also be modified prior to viewing in three distinct ways – temporarily, permanently, or system-wide.
How to Show PATH of Environment Variables in Linux?
The “PATH” environment variable is a variable that keeps a list of directories that the system explores for executable files. This allows users to run programs easily without defining the entire path to the .exe files. Additionally, users can set or update the “PATH” variable by assigning the new directory path.
This article will demonstrate various methods to show PATH environment variables in Linux.
Method 1: Using the “echo” Command
The “echo” is one of the most commands to show the “PATH” environment variables by executing with the symbol “$” in the Linux terminal:
The output shows the complete path of environment variables in the terminal.
Method 2: Using the “cat” Command
To show the PATH of the environment variable, you can execute the “cat” command with the “/etc/environment” file. Additionally, the “grep” command is utilized to fetch the value of the PATH variable and display it in the terminal:
$ cat /etc/environment | grep PATH
The command visualizes the path of an environment variable.
Optional: Update the PATH Environment Variable
To update the “PATH” variable, the “export” command is utilized to assign the new directory to the “PATH” variable. In our case, specify the path “home/itslinuxfoss” to the “PATH” as below:
$ export PATH=$PATH:/home/itslinuxfoss
In this way, a new path “/home/itslinuxfoss” is set to the “PATH” environment variable.
Method 3: Using the “env” Command
Users can utilize the “env” command that fetches the value of “PATH” environment variables via the “grep” command:
The output visualizes the complete path of an environment variable.
Method 4: Using the “set” Command
To view the PATH environment variable, you can execute the “set” command with the “grep” by specifying the “PATH” variable in Linux:
The outcome of the above command returns the “PATH” value of the environment variable.
Method 5: Using the “printenv” Command
You can view the value of the PATH variable by executing the “printenv” command:
The above execution shows the path of environment variables in linux.
Conclusion
To show the PATH of an environment variable, execute the “echo“, “cat“, “env“, “set“, and “printenv” commands with the “PATH” variable. It comprises the number of directories the system requires to execute configuration files. Additionally, users can set or update the “PATH” variable by assigning the new directory path via the “export” command. This article has explained all possible methods to show the PATH of an environment variable in Linux.