Как изменить path linux

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)

Читайте также:  Linux mint ip address

@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

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.

Читайте также:  Linux profile где лежит

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 set your $PATH variable in Linux

Telling your Linux shell where to look for executable files is easy, and something everyone should be able to do.

A path through nature

Thomas Hendele on Pixabay (CC0). Modified by Opensource.com. CC BY-SA 4.0.

Being able to edit your $PATH is an important skill for any beginning POSIX user, whether you use Linux, BSD, or macOS.

When you type a command into the command prompt in Linux, or in other Linux-like operating systems, all you’re doing is telling it to run a program. Even simple commands, like ls, mkdir, rm, and others are just small programs that usually live inside a directory on your computer called /usr/bin. There are other places on your system that commonly hold executable programs as well; some common ones include /usr/local/bin, /usr/local/sbin, and /usr/sbin. Which programs live where, and why, is beyond the scope of this article, but know that an executable program can live practically anywhere on your computer: it doesn’t have to be limited to one of these directories.

When you type a command into your Linux shell, it doesn’t look in every directory to see if there’s a program by that name. It only looks to the ones you specify. How does it know to look in the directories mentioned above? It’s simple: They are a part of an environment variable, called $PATH, which your shell checks in order to know where to look.

View your PATH

Sometimes, you may wish to install programs into other locations on your computer, but be able to execute them easily without specifying their exact location. You can do this easily by adding a directory to your $PATH. To see what’s in your $PATH right now, type this into a terminal:

You’ll probably see the directories mentioned above, as well as perhaps some others, and they are all separated by colons. Now let’s add another directory to the list.

Set your PATH

Let’s say you wrote a little shell script called hello.sh and have it located in a directory called /place/with/the/file. This script provides some useful function to all of the files in your current directory, that you’d like to be able to execute no matter what directory you’re in.

Читайте также:  Linux отключить дискретную видеокарту

Simply add /place/with/the/file to the $PATH variable with the following command:

export PATH=$PATH:/place/with/the/file

You should now be able to execute the script anywhere on your system by just typing in its name, without having to include the full path as you type it.

Set your PATH permanently

But what happens if you restart your computer or create a new terminal instance? Your addition to the path is gone! This is by design. The variable $PATH is set by your shell every time it launches, but you can set it so that it always includes your new path with every new shell you open. The exact way to do this depends on which shell you’re running.

Not sure which shell you’re running? If you’re using pretty much any common Linux distribution, and haven’t changed the defaults, chances are you’re running Bash. But you can confirm this with a simple command:

That’s the «echo» command followed by a dollar sign ($) and a zero. $0 represents the zeroth segment of a command (in the command echo $0, the word «echo» therefore maps to $1), or in other words, the thing running your command. Usually this is the Bash shell, although there are others, including Dash, Zsh, Tcsh, Ksh, and Fish.

For Bash, you simply need to add the line from above, export PATH=$PATH:/place/with/the/file, to the appropriate file that will be read when your shell launches. There are a few different places where you could conceivably set the variable name: potentially in a file called ~/.bash_profile, ~/.bashrc, or ~/.profile. The difference between these files is (primarily) when they get read by the shell. If you’re not sure where to put it, ~/.bashrc is a good choice.

For other shells, you’ll want to find the appropriate place to set a configuration at start time; ksh configuration is typically found in ~/.kshrc, zsh uses ~/.zshrc. Check your shell’s documentation to find what file it uses.

This is a simple answer, and there are more quirks and details worth learning. Like most everything in Linux, there is more than one way to do things, and you may find other answers which better meet the needs of your situation or the peculiarities of your Linux distribution. Happy hacking, and good luck, wherever your $PATH may take you.

This article was originally published in June 2017 and has been updated with additional information by the editor.

Источник

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