0

Recent instructional video I was watching said to put your export PATH="$PATH..." statement in your .profile and after sourcing manually in the terminal to append ". ~/.profile" to your .bashrc file as well. However, when I do this it messes up my terminal when I open a new tab rather than allows me to access the bin from the path.

Did this behavior change? If so, where is the correct place to source my profile so that opening a new tab or terminal window gives access to the same commands/executables in the bin folder I am referencing?

Mainly need guidance on ensuring that changes to .profile did not need to be referenced elsewhere. This was understood and picked up by the person who answered my question - marked as answer below.

Edited for clarity and to fix a typo.

3 Answers3

2

The answer is: nowhere. .profile is sourced automatically each time you log in. .bashrc is sourced each time an interactive terminal starts. There is no use in trying to source .profile once more in .bashrc, which actually causes an infinite loop on Ubuntu because .bashrc is sourced in .profile.

So far with respect to the problem you ask about, problem Y.

With respect to your real problem, X, i.e., having a bin folder in your PATH: no other action is needed than 1) creating ~/bin (and/or ~/.local/bin), then 2) log out and back in. If these directories exist, they are automatically added to your PATH (you can see the code that does that in ~/.profile). To keep things neat, do not add other directories to your PATH, although you could do so in your .profile file.

vanadium
  • 88,010
2

Ubuntu has a non-standard setup it has inherited from Debian and its .bashrc file is sourced by .profile:

$ grep -A1 bashrc /etc/skel/.profile 
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
    . "$HOME/.bashrc"
    fi

So if you then also source .profile from ~/.bashrc you get into an infinite loop, which will break your shell.

The right way to add a new directory to your $PATH is to edit ~/.profile. The file is sourced every time you start a new login shell and, on Ubuntu systems, is also sourced when you log in graphically. After modifying ~/.profile to add something to the $PATH, the change will take effect next time you log in. So instead of making ~/.bashrc source ~/.profile, just logout and log back in again and your changes will be there.

terdon
  • 100,812
-1

export PATH=$PATH':<path>:<another path>' appends paths to the PATH environment variable. It is the paths that should be used when locating executable files (programs).

If you have a program in /home/<user>/bin called 'Gloop', and you type Gloop at the command prompt in the bash shell (terminal), it normally won't execute, because /home/<user>/bin is not in the PATH variable, so bash doesn't know to look there for programs names.

So, you would go up to the first line, and insert /home/<user>/bin with your actual username, in place of <path>, and then you can type Gloop at the command line, and it will launch.

But sometimes you want to execute a program, but you don't want it in the PATH.

Then, you do ./Gloop from the directory the program is in. Why you would want to append the contents of .Profile to the PATH is beyond me. It's usually empty, so the effect would be nothing!

If you want to make your changes to the PATH environment variable persistent across reboots, put the export command, and everything else you need from the first line of this answer, and append it to the end of the file /home/<user>/.bashrc

.bashrc holds your personal bash environment. It is sourced each time you log in. Try taking another look at the video, and see what the author is trying to do with .Profile

Brian
  • 334
  • /home/<user>/bin is a bad example: Ubuntu is set up to automatically include such directory in the PATH during login if it exists (see a default .profile to see how this works). – vanadium Mar 13 '22 at 08:57
  • Yes, $USER/bin is added to the $PATH automatically on Ubuntu. And .profile is not empty by default! .bashrc is not sourced when you log in, it is sourced when you open a new interactive, non-login shell. Login shells do not source it by default, they source ~/.profile (and some other files) instead. It just happens that Ubuntu's deafult .profile also sources ~/.bashrc. See Sequence of scripts sourced upon login for more details. – terdon Mar 13 '22 at 12:04
  • I'm not used to operating systems that tuck me into bed and kiss me goodnight. Aside from the nit picking and hair splitting, you can get the general idea. We're not in second grade anymore! .Profile is normally empty. It might not be the current fashion in Ubtuntu, but these fashions come and go. .Plan and .Profile are for use by the user! It's idiotic to use them for that. And, Linux normally sources the bash environment when it finds who you are, at login! How else are you supposed to do anything with the shel!? You might want to check on those things. – Brian Mar 18 '22 at 09:15
  • Or are you talking about GUI greeter login? – Brian Mar 18 '22 at 09:17
  • Could someone just delete my long comment. I guess I can't do that. – Brian Mar 18 '22 at 09:24