bash 如何在 ubuntu 的 shell 脚本中运行 .profile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22913551/
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 run .profile inside a shell script in ubuntu
提问by Prats
I am running a script which is echoing the environment variables in .profile file and than I am running the script, but I am getting following error
我正在运行一个脚本,它在 .profile 文件中回显环境变量,而不是运行脚本,但出现以下错误
I tried following:
我尝试了以下操作:
node@node-virtual-machine:~$ cat env.sh
#!/bin/bash
echo 'export JAVA_HOME=/home/node/jdk1.6.0_45' >> /home/node/.profile
echo 'export PATH=$PATH:$JAVA_HOME/bin' >> /home/node/.profile
cd /home/node/
source .profile
node@node-virtual-machine:~$ sh env.sh
sudo: source: command not found
How to execute .profile within a script?
如何在脚本中执行 .profile?
采纳答案by anubhava
Instead of:
代替:
sh env.sh
You should run:
你应该运行:
bash ./env.sh
Besides instead of:
除了代替:
source .profile
use:
用:
source ~/.profile
回答by John Zwinck
Your script says /bin/bash
at the top but you run it with sh
which is probably dash
on your system. You probably are already running Bash at the prompt, so you should say source env.sh
instead of sh env.sh
if you want the variables to be exposed to your terminal.
您的脚本/bin/bash
在顶部显示,但您使用sh
它运行它可能dash
在您的系统上。您可能已经在提示符下运行 Bash,因此您应该说,source env.sh
而不是sh env.sh
您是否希望将变量暴露给您的终端。
回答by John Zwinck
If .profile
exists as /home/node/.profile
, you have all that is necessary.
如果.profile
存在为/home/node/.profile
,则您拥有所有必要的信息。