0

This question already has an answer here:

In an online video lecture the teacher has explained how to add to PATH both on the command line and via .bashrc, and has indicated that the proper way of doing this is through .bashrc but has not adequately explained why.

My question is what is the difference between executing the following from the command line:

$ export PATH=/home/username/bin:$PATH

Or simply adding the line: export PATH=/home/username/bin:$PATH to my ~/.bashrc.

marked as duplicate by Jesse_b, Archemar, Jeff Schaller, Vlastimil, meuh Feb 26 '18 at 8:14

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @Jesse_b Thanks for the edit.It makes more sense of what I wanted to ask. – Sagittarius009 Feb 25 '18 at 17:54
2

PATH is an environment variable (one of many) that stores the path that Bash searches when you type in a command on the command line. If you add a folder to the PATH, then any commands in that folder will be available for you to type at the command line (although, be careful, if the same commands exist elsewhere, earlier in the path).

However, if you change PATH on the command line, that won't persist outside of that session, after you log out. The .bashrc script is run every time Bash starts, so if you put a command in there to edit the PATH (or any other environment variable), that change will persist every time you log in to Bash.

Does that help?

  • So the path to a directory I set is for that session only. But if I put the same in .bashrc it is permanent.Did I get it right ? – Sagittarius009 Feb 25 '18 at 17:42
  • @user7841468 essentially, yes. In general, environment variables are internal to Bash and not persistent. If you change one from the command line, that will only last for that session. However, all of the commands in .bashrc are run every time you log in to Bash, which effectively makes them persistent. – Time4Tea Feb 25 '18 at 17:51
  • That seems correct thanks for the clarification.You were a great help sir. – Sagittarius009 Feb 25 '18 at 17:53
0

To modify the PATH in such a way that future shells see the modified value, the change should be made in the shell startup files (~/.bashrc in the case of bash for example).

However, that change will not be visible in the current shell, so executing the equivalent command on the command line introduces the new value for PATH for the currently running shell session as well.

If you only changed the shell startup file, you would have to close the current shell session and start a new one to see the effect of the change.

Not the answer you're looking for? Browse other questions tagged or ask your own question.