【发布时间】:2011-02-20 20:43:47
【问题描述】:
如何在 Bash 脚本中重新加载环境变量。我在同一个脚本中更新 ~/.bashrc 并希望这些更改得到反映。
我尝试过使用
source ~/.bashrc
以下是我的脚本
#!/bin/bash
echo $PATH
echo "export PATH=\$PATH:/home/user/test" >> ~/.bashrc
source ~/.bashrc
echo $PATH
两个回显都返回相同的 $PATH 值。但是当我在另一个 bash 实例上运行 echo $PATH 时,它会返回新添加的路径。
任何帮助将不胜感激。
谢谢。
以下是我的 .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)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[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
【问题讨论】:
-
你的 .bashrc 是运行到最后,还是提前退出?您应该显示此文件的内容。
-
我已经添加了我现有的 bashrc 文件的内容。
-
我没有看到这个脚本底部的 /home/user/test 行,是否有附加到文件的错误,例如写权限?
-
这是运行脚本之前的 bashrc。