Linux profile add path

How to correctly add a path to PATH?

I’m wondering where a new path has to be added to the PATH environment variable. I know this can be accomplished by editing .bashrc (for example), but it’s not clear how to do this. This way:

If there are already some paths added, e.g. PATH=$PATH:$HOME/.local/bin:$HOME/bin , another can be added by separating with a : e.g. PATH=$PATH:$HOME/.local/bin:$HOME/bin:/home/ec2-user/pear/bin .

12 Answers 12

The simple stuff

depending on whether you want to add ~/opt/bin at the end (to be searched after all other directories, in case there is a program by the same name in multiple directories) or at the beginning (to be searched before all other directories).

You can add multiple entries at the same time. PATH=$PATH:~/opt/bin:~/opt/node/bin or variations on the ordering work just fine. Don’t put export at the beginning of the line as it has additional complications (see below under “Notes on shells other than bash”).

If your PATH gets built by many different components, you might end up with duplicate entries. See How to add home directory path to be discovered by Unix which command? and Remove duplicate $PATH entries with awk command to avoid adding duplicates or remove them.

Some distributions automatically put ~/bin in your PATH if it exists, by the way.

Where to put it

Put the line to modify PATH in ~/.profile , or in ~/.bash_profile or if that’s what you have. (If your login shell is zsh and not bash, put it in ~/.zprofile instead.)

The profile file is read by login shells, so it will only take effect the next time you log in. (Some systems configure terminals to read a login shell; in that case you can start a new terminal window, but the setting will take effect only for programs started via a terminal, and how to set PATH for all programs depends on the system.)

Note that ~/.bash_rc is not read by any program, and ~/.bashrc is the configuration file of interactive instances of bash. You should not define environment variables in ~/.bashrc . The right place to define environment variables such as PATH is ~/.profile (or ~/.bash_profile if you don’t care about shells other than bash). See What’s the difference between them and which one should I use?

Don’t put it in /etc/environment or ~/.pam_environment : these are not shell files, you can’t use substitutions like $PATH in there. In these files, you can only override a variable, not add to it.

Читайте также:  Солид воркс на линуксе

Potential complications in some system scripts

You don’t need export if the variable is already in the environment: any change of the value of the variable is reflected in the environment.¹ PATH is pretty much always in the environment; all unix systems set it very early on (usually in the very first process, in fact).

At login time, you can rely on PATH being already in the environment, and already containing some system directories. If you’re writing a script that may be executed early while setting up some kind of virtual environment, you may need to ensure that PATH is non-empty and exported: if PATH is still unset, then something like PATH=$PATH:/some/directory would set PATH to :/some/directory , and the empty component at the beginning means the current directory (like .:/some/directory ).

if [ -z "$" ]; then export PATH=/usr/local/bin:/usr/bin:/bin; fi 

Notes on shells other than bash

In bash, ksh and zsh, export is special syntax, and both PATH=~/opt/bin:$PATH and export PATH=~/opt/bin:$PATH do the right thing even. In other Bourne/POSIX-style shells such as dash (which is /bin/sh on many systems), export is parsed as an ordinary command, which implies two differences:

  • ~ is only parsed at the beginning of a word, except in assignments (see How to add home directory path to be discovered by Unix which command? for details);
  • $PATH outside double quotes breaks if PATH contains whitespace or \[*? .

So in shells like dash, export PATH=~/opt/bin:$PATH sets PATH to the literal string ~/opt/bin/: followed by the value of PATH up to the first space. PATH=~/opt/bin:$PATH (a bare assignment) doesn’t require quotes and does the right thing. If you want to use export in a portable script, you need to write export PATH=»$HOME/opt/bin:$PATH» , or PATH=~/opt/bin:$PATH; export PATH (or PATH=$HOME/opt/bin:$PATH; export PATH for portability to even the Bourne shell that didn’t accept export var=value and didn’t do tilde expansion).

¹ This wasn’t true in Bourne shells (as in the actual Bourne shell, not modern POSIX-style shells), but you’re highly unlikely to encounter such old shells these days.

Источник

How to Add a Path Permanently in Linux

This Linux tutorial explains how to add a path permanently in Linux. It is optimized for both users who are looking for a fast practical answer and for users who are looking for understanding both global and user environment variables.

The tips provided in this article include two methods to add the persistent PATH for both specific and all users, being useful for every Linux distribution.

The first two sections of the content go straight to the point, describing the steps to add a path permanently. After which, you can find a short explanation on environment variables.

All instructions explained in this article contain screenshots, making it easy to understand and execute the examples.

How to Add a PATH Permanently to a Specific User in Linux

The first step before we start is to check our current PATH environment variable.

Читайте также:  Visual studio 2019 linux

You can do it in a simple way by executing the echo command followed by a dollar sign ($) and the environment variable whose value you want to see which, in this case is the PATH, as shown in the following screenshot:

As you can see, there are 6 paths separated by colon. All paths must be separated by colon.

Another way to check all your environment variables including PATH is by running the env command as shown in the following example:

NOTE: While writing this tutorial, I edited my paths several times. That’s why you will see different values in the screenshots.

As you can see in the given figure, all environment variables are listed including the user paths.

To add a permanent path, one of the methods is to edit the hidden file .bashrc. You can print the hidden files by running the ls command followed by the –a (All) flag in the home directory.

To edit the .bashrc file, add a permanent path. Use the text editor of your preference. In my case, I used nano, as shown in the following:

In this first example, I added the fictitious path /home/linuxhint/something/default/bin where linuxhint is the user home.

The syntax is the following, where must be replaced with the actual path that you want to add:

In this case, I added the following line:

Alternatively, you can use the echo command to append the line using the following syntax where ‘Line Content’ must be replaced with the full command. And File must be replaced with the file you are editing (.bashrc).

The practical example is the following, as stated previously, in case I want to add the path $HOME/something/default/bin or /home/linuxhint/something/default/bin (which are the same):

Update your environment variables by running the following command:

To print all environment variables including updated paths, you can use the env command.

To show only the PATH environment variable, run the command shown in the following figure:

Another way to add a path to the user environment variable is by editing the “.profile” file located in the home directory.

This time, let’s use the ls command followed by the –l flag to show all files including the hidden files.

As you can see, there is a file named “.profile”. Edit it using the text editor of your choice. In my case, I used nano.

Find the line similar to the one pointed by the white arrow in the following figure:

In this example, I added the /home/linuxhint/something2/default/bin path.

Below the found line, add a line as shown at the end of the following image, replacing the /home/linuxhint/something2/default/bin with the actual path that you want to add:

Update your environment variable by executing the command shown in the following image:

Check the updated path with the following command:

Читайте также:  Linux reload user groups

Or print all the environment variables including PATH using the env command:

You can find the instructions to add a permanent path for all users in the following discussion.

How to Globally Add a Path Permanently to All Users in Linux

This section shows how to add a global persistent path environment variable for all users.

This can be done by editing two files, /etc/profile and /etc/bash.bashrc.

In the first example, I will show you how to edit the /etc/profile file.

Use the text editor that you used to modify the /etc/profile.

At the end of the file, add the following line where /opt/something10/bin must be replaced with the path that you want to add.

Update the environment variable PATH using the source command as done in the previous section of this article.

Check if the path was properly added using the echo command as shown in the following image:

Another way to permanently add a path globally is by editing the /etc/bash.bashrc file using the text editor to open it.

In the following example, I added the new path /opt/something20/bin.

Append a similar line as the following, replacing the /opt/something20/bin with the actual path that you want to add.

Update the PATH environment variable using the source command. Then, check it by running the echo command as shown in the following example:

As you can see, both methods worked successfully and the paths were added.

User vs System Wide Environment Variables

The difference between the user specific and the global environment variables is the following:

  • User Environment Variables: User environment variables like PATH are defined in the user home configuration files. They are loaded from the home directory when the user starts a session.
  • System Wide Variables: This type of variables don’t belong to a specific user, but to the whole system affecting all users.

This tutorial deeply explained how to add a PATH variable. To edit the user environment variables, you need to edit the .bashrc or .profile files located in the home directory. The variables exported to this file will load every time the user starts a session.

The system variables are stored in the /etc/bash.bashrc or /etc/profile files.

Conclusion

As you can see, adding the PATH variables both temporarily and permanently is pretty easy and can be done by any Linux user independently of the knowledge level. Every Linux user must understand the PATH variable function and how to manage it. Other variables are also deeply explained at Linux Hint. The content previously explained is valid for almost every Linux distribution.

Thank you for reading this tutorial showing how to permanently add the PATH variable. Keep following us for more professional content.

About the author

David Adams

David Adams is a System Admin and writer that is focused on open source technologies, security software, and computer systems.

Источник

Оцените статью
Adblock
detector