[linux]How to set PATH in shell script, and keep it avaiable even after it exits

Now and then, maybe you want to use bash shell script to do things.

There may be a while you want to set PATH, and even after exiting the shell script, you still want the PATH keep that value.

Here, http://www.linuxquestions.org/questions/linux-newbie-8/write-shell-script-to-change-path-4175417178/

and the last post by David the H.(a bash Guru) is very very useful, which has been pasted as follows,

PATH, being an environment setting, can only be set from inside the shell that uses it. This means that you cannot use any external scripts to change it. The command in the script only changes the script's environment, which is lost when it closes.

I wrote up a detailed explanation of how the environment works the last time someone asked about this. You might have found it if you had done a search before posting.

http://www.linuxquestions.org/questi...2/#post4725166

The only ways you can change the PATH, other than manually, are through A) sourcing, which imports the code into the current shell's environment and executes the commands there, or B) through the use of a shell function, which is like a script, but sits inside the current environment and therefore can affect it directly.

http://mywiki.wooledge.org/BashGuide...ands#Functions
 

That's all, thanks.

原文地址:https://www.cnblogs.com/lake-of-embedded-system/p/3374247.html