2016-12-28 34 views
3

可以为以下命令临时设置PATH。另外,PATH可以在脚本中设置,该脚本永久保存(在该终端或会话中)。如何在脚本中设置全局有效的PATH,而不是脚本完成后?如何只在bash脚本中临时设置PATH?

例子:

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 

回答

6

这是不正确的。 如果你写一个脚本,并改变$ PATH变量,变化只生活在脚本:

vi test.sh 

里面的文件:

#!/bin/bash 

export PATH="$PATH: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 

相同输出。该更改仅在脚本内有效!

+0

呃......看来我完全错了......但是为什么在'.bashrc永久导出'? (对不起,这可能是另一个问题) – akai

+2

@akai'.bashrc'文件来自与启动的shell相同的环境。这不是一个在自己的环境中运行的脚本。 – Kusalananda