在 Bash 脚本中重新加载环境变量

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

Reload Environment Variables in a Bash Script

unixbash

提问by GeekTantra

How can I reload the environment variables in a Bash Script. I am updating ~/.bashrc in the same script and want the changes to get reflected.

如何在 Bash 脚本中重新加载环境变量。我正在同一脚本中更新 ~/.bashrc 并希望更改得到反映。

I have tried using

我试过使用

source ~/.bashrc

The following is my script

以下是我的脚本

#!/bin/bash
echo $PATH 
echo "export PATH=$PATH:/home/user/test" >> ~/.bashrc
source ~/.bashrc
echo $PATH

Both the echos return the same value of $PATH. But when I run echo $PATH on another instance of bash it returns the newly added path.

两个回声都返回相同的 $PATH 值。但是当我在另一个 bash 实例上运行 echo $PATH 时,它返回新添加的路径。

Any help would be appreciated.

任何帮助,将不胜感激。

Thanks.

谢谢。

The following is the content of my .bashrc file (without the modifications from the script)

以下是我的 .bashrc 文件的内容(没有脚本的修改)

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

# If not running interactively, don't do anything

[ -z "$PS1" ] && return

HISTCONTROL=ignoredups:ignorespace

shopt -s histappend

HISTSIZE=1000
HISTFILESIZE=2000

shopt -s checkwinsize

[ -x /usr/bin/lesspipe ] && eval "$(SHELL=/bin/sh lesspipe)"

if [ -z "$debian_chroot" ] && [ -r /etc/debian_chroot ]; then
    debian_chroot=$(cat /etc/debian_chroot)
fi

case "$TERM" in
    xterm-color) color_prompt=yes;;
esac

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
  color_prompt=yes
    else
  color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[3[01;32m\]\u@\h\[3[00m\]:\[3[01;34m\]\w\[3[00m\]$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w$ '
fi
unset color_prompt force_color_prompt

case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac

if [ -x /usr/bin/dircolors ]; then
    test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)"
    alias ls='ls --color=auto'
    #alias dir='dir --color=auto'
    #alias vdir='vdir --color=auto'

    alias grep='grep --color=auto'
    alias fgrep='fgrep --color=auto'
    alias egrep='egrep --color=auto'
fi

alias ll='ls -alF'
alias la='ls -A'
alias l='ls -CF'

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

回答by Jonathan Leffler

Your .bashrc starts:

你的 .bashrc 开始:

# If not running interactively, don't do anything
[ -z "$PS1" ] && return

Since your script does not have PS1 set (because it is not interactive), it doesn't reset path because it exits early - exactly as B Mitchsuggested. To demonstrate, modify your script:

由于您的脚本没有设置 PS1(因为它不是交互式的),它不会重置路径,因为它提前退出 - 正如B Mitch建议的那样。为了演示,请修改您的脚本:

#!/bin/bash
echo $PATH 
echo "export PATH=$PATH:/home/user/test" >> ~/.bashrc
PS1='$ '
source ~/.bashrc
echo $PATH