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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:08:33  来源:igfitidea点击:

How to run .profile inside a shell script in ubuntu

bashshellsh

提问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/bashat the top but you run it with shwhich is probably dashon your system. You probably are already running Bash at the prompt, so you should say source env.shinstead of sh env.shif 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 .profileexists as /home/node/.profile, you have all that is necessary.

如果.profile存在为/home/node/.profile,则您拥有所有必要的信息。