在 bash shell 脚本中重新加载 .profile(在 unix 中)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3274397/
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-09 19:29:11  来源:igfitidea点击:

Reload .profile in bash shell script (in unix)?

bashshellunix.bash-profile

提问by Amir Rustamzadeh

I'm new to bash shell scripting, and have come across a challenge. I know I can reload my ".profile" file by just doing:

我是 bash shell 脚本的新手,遇到了一个挑战。我知道我可以通过执行以下操作来重新加载我的“.profile”文件:

. .profile

but I'm trying to execute the same in a bash script I'm writing and it is just not working. Any ideas? Anything else I can provide to clarify?

但我试图在我正在编写的 bash 脚本中执行相同的操作,但它不起作用。有任何想法吗?我还能提供什么来澄清?

Thanks

谢谢

回答by kofriel

Try this to reload your current shell:

试试这个来重新加载你当前的 shell:

source ~/.profile

回答by girardengo

Try this:

尝试这个:

cd 
source .bash_profile

回答by twboc

A couple of issues arise when trying to reload/source ~/.profile file. [This refers to Ubuntu linux - in some cases the details of the commands will be different]

尝试重新加载/源 ~/.profile 文件时会出现一些问题。[这里指的是 Ubuntu linux - 在某些情况下命令的细节会有所不同]

  1. Are you running this directly in terminal or in a script?
  2. How do you run this in a script?
  1. 你是直接在终端还是在脚本中运行它?
  2. 你如何在脚本中运行它?

Ad. 1)

广告。1)

Running this directly in terminal means that there will be no subshell created. So you can use either two commands:

直接在终端中运行它意味着不会创建子shell。所以你可以使用两个命令:

source ~/.bash_profile

or

或者

. ~/.bash_profile

In both cases this will update the environment with the contents of .profile file.

在这两种情况下,这都将使用 .profile 文件的内容更新环境。

Ad 2) You can start any bash script either by calling

广告 2) 您可以通过调用来启动任何 bash 脚本

sh myscript.sh 

or

或者

. myscript.sh

In the first case this will create a subshell that will not affect the environment variables of your system and they will be visible only to the subshell process. After finishing the subshell command none of the exports etc. will not be applied. THIS IS A COMMON MISTAKE AND CAUSES A LOT OF DEVELOPERS TO LOSE A LOT OF TIME.

在第一种情况下,这将创建一个不会影响系统环境变量的子外壳,并且它们仅对子外壳进程可见。完成 subshel​​l 命令后,将不会应用任何导出等。这是一个常见的错误,会导致许多开发人员损失大量时间。

In order for your changes applied in your script to have effect for the global environment the script has to be run with

为了使您在脚本中应用的更改对全局环境生效,脚本必须运行

.myscript.sh

command.

命令。

In order to make sure that you script is not runned in a subshel you can use this function. (Again example is for Ubuntu shell)

为了确保您的脚本不在 subshel​​ 中运行,您可以使用此功能。(再次以 Ubuntu shell 为例)

#/bin/bash

preventSubshell(){
  if [[ $_ != 
#!/bin/bash
# .... some previous code ...
# help set exec | less
set -- 1 2 3 4 5  # fake command line arguments
exec bash --login -c '
echo ##代码##
echo $@
echo my script continues here
' arg0 "$@"
]] then echo "Script is being sourced" else echo "Script is a subshell - please run the script by invoking . script.sh command"; exit 1; fi }

I hope this clears some of the common misunderstandings! :D Good Luck!

我希望这能消除一些常见的误解!:D 祝你好运!

回答by Ignacio Vazquez-Abrams

The bash script runs in a separate subshell. In order to make this work you will need to source this other script as well.

bash 脚本在单独的子shell 中运行。为了完成这项工作,您还需要获取其他脚本的源代码。

回答by tilo

Try:

尝试:

##代码##