bash 如何暂时仅在bash脚本中设置PATH?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41358018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set PATH only in bash script temporarily?
提问by akai
PATH can be set temporarily for following commands. Also, PATH can be set in a script, which remains permanently (in that terminal, or session). How can I set PATH that is globally effective in the script but not after the script finishes?
PATH 可以为以下命令临时设置。此外,PATH 可以在脚本中设置,该脚本永久保留(在该终端或会话中)。如何设置在脚本中全局有效但在脚本完成后无效的 PATH?
Example:
例子:
PATH=$PATH:/path1 path1 is valid only for this line
export PATH=$PATH:/path2
path2 is valid for this line
and this line too
I like it
exit
> path2 is still valid after the script finishes
> and when I type commands here manually
> I don't like it
回答by m47730
it's simply not true. If you write a script and change the $PATH variable, the change live only in the script:
这根本不是真的。如果您编写脚本并更改 $PATH 变量,则更改仅存在于脚本中:
vi test.sh
inside the file:
文件内:
#!/bin/bash
export PATH="$PATH:test"
let's test:
让我们测试一下:
echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/matteo/.local/bin:/home/matteo/bin:./bin:/home/matteo/.local/bin:/home/matteo/bin:./bin
chmod ug+x test
./test
echo $PATH
/usr/lib64/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/home/matteo/.local/bin:/home/matteo/bin:./bin:/home/matteo/.local/bin:/home/matteo/bin:./bin
same output. The change is effective only inside the script!
相同的输出。更改仅在脚本内部有效!