1

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.

jesse_b
  • 37,005
shuberman
  • 235
  • 1
  • 2
  • 9

2 Answers2

3

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?

Time4Tea
  • 2,366
  • 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 ? – shuberman 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. – shuberman 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.

Kusalananda
  • 333,661