bash/cygwin/$PATH: Do I really have to reboot to alter $PATH?

寵の児 提交于 2019-12-10 15:12:21

问题


I wanted to use the Python installed under cygwin rather than one installed under WinXP directly, so I edited ~/.bashrc and sourced it. Nothing changed. I tried other things, but nothing I did changed $PATH in any way. So I rebooted. Aha; now $PATH has changed to what I wanted.

But, can anyone explain WHY this happened? When do changes to the environment (and its variables) made via cygwin (and bash) take effect only after a reboot?

(Is this any way to run a railroad?) (This question is unlikely to win any points, but I'm curious, and I'm also tired of wading through docs which don't help on this point.)


回答1:


Try:

PATH="${PATH}:${PYTHON}"; export PATH

Or:

export PATH="${PATH}:${PYTHON}"

the quotes preserve the spaces and newlines that you don't have in your directory names. I repeat "don't".

If you want to change the path for the current environment and any subsequent processes, use something similar to either of the commands above; they are equivalent.

If you want to change the path for the next time you start Bash, edit ~/.bashrc and add one of the above (for example) or edit the existing PATH setting command that you find there.

If you want to affect both the current environment and any subsequent ones (i.e. have an immediate and a "permanent" affect), edit ~/.bashrc and do one of the following: type one of the first two forms shown above or source the ~/.bashrc file. Sometimes, you may not want to do the sourcing if, for example, it would undo some temporary thing that you're making use of currently like have some other variables set differently than ~/.bashrc would set (reset) them to.

I don't think you need to worry about hash unless you're either doing some serious rearranging or adding some local replacements for system utilities perhaps.




回答2:


If you want your changes to be permanent, you should modify the proper file (.bashrc in this case) and perform ONE of the following actions:

  • Restart the cygwin window
  • source .bashrc (This should work, even if is not working for you)
  • . .bashrc (that is dot <space> <filename>)

However, .bashrc is used by default when using a BASH shell, so if you are using another shell (csh, ksh, zsh, etc) then your changes will not be reflected by modifying .bashrc.




回答3:


A couple of things to try and rule out at least:

  1. Do you get the same behavior as the following from the shell? (Pasted from my cygwin, which works as expected.)

    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin
    
    $ export PATH=$PATH:/cygdrive/c/python/bin
    
    $ echo $PATH
    /usr/local/bin:/usr/bin:/bin:/cygdrive/c/python/bin
    
  2. Is your bashrc setting the PATH in a similar way to the above? (i.e. the second command).

  3. Does your bashrc contain a "source" or "." command anywhere? (Maybe it's sourcing another file which overwrites your PATH variable.)




回答4:


You may need to re-initialize bash's hashes after modifying the path variable:

hash -r


来源:https://stackoverflow.com/questions/1122924/bash-cygwin-path-do-i-really-have-to-reboot-to-alter-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!