How to run .profile inside a shell script in ubuntu

孤街醉人 提交于 2019-12-12 14:11:48

问题


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

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?


回答1:


Instead of:

sh env.sh

You should run:

bash ./env.sh

Besides instead of:

source .profile

use:

source ~/.profile



回答2:


If .profile exists as /home/node/.profile, you have all that is necessary.




回答3:


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.



来源:https://stackoverflow.com/questions/22913551/how-to-run-profile-inside-a-shell-script-in-ubuntu

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