Linux change path permanently

How do I modify my PATH so that the changes are available in every Terminal session

I want to add a directory to search my search path. I know I have to modify the PATH environment variable. However, I want the change to be permanent, so that it is always in effect, for every Terminal (bash) window I open. There is an overload of confusing and possibly conflicting information in https://help.ubuntu.com/community/EnvironmentVariables I am using Ubuntu 10.04. Suppose I want to add /usr/local/foo to my PATH . Which file ( .bashrc , .profile , .bash_login , etc. ) should I modify and what should the new line(s) look like?

10 Answers 10

The following command adds a path to your current path:

export PATH=$PATH:/my/custom/path 

If you want your setup to execute this command every time, there are a number of places where you can put it. When you login, the following scripts will be executed in this order:

/etc/profile (which starts by loading everything in /etc/profile.d) ~/.profile (which starts by loading ~/.bashrc if you are running bash) 
  • ~/.profile is only loaded if ~/.bash_profile and ~/.bash_login DO NOT EXIST. Otherwise, at least bash, will load them instead. It is advisable to use .profile and not the bash specific scripts. So, if in these attempts you created .bash_login , please delete it now.
  • ~/.bashrc is only loaded if you are running an interactive session. (something with a prompt where you can actually type something).
  • ~/.bashrc is loaded again and again, every time you open up a new terminal. So a new tab in gnome-terminal, a new virtual terminal, etc. So even if you don’t login again, .bashrc is loaded (and thereby resets its environment) every time you open a new shell.
  • Things like byobu should really go into .profile , (otherwise it won’t work 😉
  • Things like paths should go into .profile if you want them to work outside of the interactive sessions. (say when you press Alt + F2 in GNOME)

I’ll mark this as the answer if you update it to include the requested export line that should be added to .profile.

This used to be valid only for console logins (e.g. ssh, or the virtual terminals accessible for Ctrl+Alt+Fx). I didn’t know that /etc/gdm/Xsession sources ~/.profile these days. Neat!

to make this answer more comprehensive please add MattH’s comment about sourcing ~/.profile to activate changes without a logoff/on cycle.

@JoshuaFlanagan: export is not necessary for PATH . PATH is already an environment variable (as opposed to a shell variable)

@schwiz: ~/.profile is not executed on each terminal, it is executed before, when your desktop session starts. The one executed on every terminal is ~/.bashrc

Читайте также:  Linux to cmd commands

I got it to work by modifying ~/.profile

It looks like adding ~/bin to my path was a bad example, as there is already code in ~/.profile to do that automatically, if the directory exists.

To add the usr/local/foo directory to my path for every session going forward, I add/edit the following line at the end of my .profile:

export PATH=$PATH:/usr/local/foo 

However, to make this take effect, I needed to log out and log back in (simply closing the Terminal window and opening a new one did NOT work).

Make that export PATH=»$PATH:/usr/foo» , in case you ever have spaces or other special characters in $PATH .

@MattH: no you can’t. if you source ~/.profile in a given terminal, it will be effective for that terminal only

@MestreLion — you are right. I was mentioning it for convenience for the current terminal. Forgot to add that.

What if I already have something in PATH? Could I append to it like PATHS work in Windows? For example I have PATH=»$HOME/bin:$HOME/.local/bin:$PATH» already.

To reload .profile and take changes effects without logout/login, run:

You can add the path to /etc/environment , but be aware that no shell expansions will work; the variable will be set to literally the characters you enter.

Out of the two methods(adding export command in .profile and adding full path name to PATH in etc/environment), which should be preferred?

Before setting a PATH variable, you need to understand why you are setting the PATH variable. The Path variable is where the system tries to search when you issue some command in your terminal.

Example: whereis ls command shows ls is there inside /bin . The ls command only works if /bin is registered in the path variable.

echo $PATH gives the currently registered locations. If you want to add another custom location to your path variable there are several ways you can try.

export PATH="$PATH:/someLocation"
export PATH="$PATH:/someLocation"

Add this line into the .bashrc file present in your home folder. Which is called every time a new bash shell is created. This means you get a new Path variable exported every time a new terminal is opened. But this variable is created for only bash shells. You can use the old Path variable in other shells(ksh, sh, ssh ..).

export PATH="$PATH:/someLocation"

Add this line into the .profile file present in your home folder. Which is called every time you log in. This means you get a new Path variable exported every time your session is created. Which is available everywhere.

If you can’t find the .profile or .bashrc file in your home folder, try to create a new one. Sometimes these files won’t be created by the system.

ps: Works in ubuntu. Open to any corrections.

Источник

How to Permanently Set $PATH in Linux

Unlocking the full potential of your Linux system involves understanding the importance of the $PATH variable, which serves as a roadmap for your operating system to locate and execute commands swiftly and effortlessly. However, if you find yourself repeatedly setting the $PATH every time you log in or reboot, it is time to learn how to permanently configure it.

Читайте также:  Software linux debian free

In this comprehensive guide, we will walk you through the step-by-step process of setting the $PATH variable in Linux, ensuring that your customizations persist across sessions.

What is a $PATH Variable in Linux

The $PATH variable is a fundamental part of the Linux operating system, acting as a reference to locate executable files. It consists of a list of directories separated by colons (:) , with the system searching for commands in these directories when they are executed. Understanding the importance of the $PATH variable is crucial to effectively manage your command-line experience.

There are multiple approaches to permanently setting the $PATH variable in Linux and the two most common methods involve:

Each method has its advantages and disadvantages, so choose the one that best suits your needs. The PATH variable can also be set on a per-user basis or system-wide for all user accounts.

Check $PATH Variables in Linux

You can see your $PATH variables by running the following echo command:

Check $PATH Variable in Linux

Add a Directory to PATH Variable Linux

To temporarily add a directory, for example, /opt/sysadmin/scripts to your path, you can run the following command:

$ PATH=$PATH:/opt/sysadmin/scripts $ echo $PATH

Add Directory to PATH in Linux

One way to permanently set the $PATH variable is by modifying shell configuration files, such as .bashrc or .bash_profile . These files are executed each time you start a new shell session.

$ nano ~/.bashrc OR $ vim ~/.bashrc

The .bashrc File in Linux

Locate the line that begins with «export PATH attachment_8239″ aria-describedby=»caption-attachment-8239″ style=»width: 831px» > The .bashrc File in Linux

If you are unable to find this line, you can add it manually inside the file at the bottom of the page. As an example, I am adding the /opt/sysadmin/scripts directory path to my PATH using the command given below:

export PATH="$PATH: /opt/sysadmin/scripts"

Permanently Add Directory to PATH in Linux

To apply the changes, either open a new terminal or run the following command:

Note: The above method only works for user accounts.

Permanently Set $PATH for All Users

To permanently set system PATH for all users on the system, append the following lines at the end of the /etc/profile file. On some Linux distros, you can also use the /etc/bash.bashrc file (if it exists) but it should be sourced in /etc/profile for changes in it to work.

PATH="$PATH:/opt/sysadmin/scripts" export $PATH OR $export PATH="$PATH:/opt/sysadmin/scripts"

Alternatively, instead of making changes directly in the /etc/profile (which is not recommended), you can create a script (ending with the .sh extension) under the directory /etc/profile.d (if it exists) and make the changes in that script.

$ sudo vim /etc/profile.d/set_system_path.sh

Append the following line in it:

export PATH="$PATH:/opt/sysadmin/scripts"

Next, source the ~/.bashrc or /etc/profile or /etc/bash.bashrc (depending on the one you used), for the changes to take effect.

$ source ~/.bashrc OR $ source /etc/profile OR $ source /etc/bash.bashrc

Then confirm that your PATH has been set correctly:

$ echo $PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/sysadmin/scripts:/opt/sysadmin/scripts

Set PATH Variable in /etc/environment File

Another approach to permanently setting the $PATH a variable is by using environment-specific files. Two common files used for this purpose are /etc/environment and /etc/profile.d/.

Читайте также:  Linux rdp printer redirection

Next, locate the line that begins with «PATH /opt/sysadmin/scripts:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/>

To apply the changes, reboot your system or run the following command:

After making modifications to the $PATH variable, it’s crucial to verify that the changes have taken effect.

Check PATH in Linux

This will display the updated $PATH variable, including the directories you added. Ensure that the desired directories are now part of the $PATH variable.

Conclusion

To make the most of your Linux system, it’s a must for you to understand the $PATH variable that acts like a map and helps your computer find and run commands quickly.

If you are tired of setting $PATH every time you log in or restart your computer, better to set it permanently using the step-by-step guidelines presented in the above article.

If you have managed to do this process, you will have a better command-line experience on your Linux system.

Источник

How to Change the Path Variable in Linux

wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time.

This article has been viewed 221,348 times.

Operating systems commonly use environment variables to define various global settings for parts of your operating system or to control how applications run. The PATH variable is one of these environment variables and is constantly used without the user realizing it. The variable stores a list of directories where applications (most commonly, your shell) should look for a program whenever you run it as a command.

Image titled Change the Path Variable in Linux Step 1

  • uzair@linux:~$ echo $PATH/home/uzair/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/games
  • Note: Linux $PATH responds with «:» separators between entries.

Image titled Change the Path Variable in Linux Step 2

Image titled Change the Path Variable in Linux Step 3

  • uzair@linux:~$ echo $PATH/home/uzair/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
  • Remember, the above is only temporary and will be lost at reboot.

Image titled Change the Path Variable in Linux Step 4

Image titled Change the Path Variable in Linux Step 5

Community Q&A

To change the PATH variable, type export PATH=»$PATH:/path/to/new/executable/directory». Or, if you want to make it permanent, edit the .bashrc to say something like this: # what this location has export PATH=»$PATH:/path/to/new/executable/directory»

Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow

Источник

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