Ubuntu 14.04 LTS Bash 版本 4.3.11(1)-发布
我已将 $JAVA_HOME 添加到 ~/.profile (和 .bash_profile),如下所示:
#
# This is the default standard .profile provided to sh users.
# They are expected to edit it to meet their own needs.
#
# The commands in this file are executed when an sh user first
# logs in.
#
# $Revision: 1.10 $
#
# Set the interrupt character to Ctrl-c and do clean backspacing.
if [ -t 0 ]
then
stty intr '^C' echoe
fi
# Set the TERM environment variable
eval `tset -s -Q`
# Set the default X server.
if [ ${DISPLAY:-setdisplay} = setdisplay ]
then
if [ ${REMOTEHOST:-islocal} != islocal ]
then
DISPLAY=${REMOTEHOST}:0
else
DISPLAY=:0
fi
export DISPLAY
fi
# List files in columns if standard out is a terminal.
ls() { if [ -t ]; then /bin/ls -C $*; else /bin/ls $*; fi }
export JAVA_HOME=$(/usr/bin/java)
export PATH=$JAVA_HOME/jre/bin:$PATH
但仍然输入 echo $JAVA_HOME 会产生以下结果:
XXX:~$ echo $JAVA_HOME
JAVA_HOME /usr/local/lib/jdk-8u25/
答案1
您使用了:
export JAVA_HOME=$(/usr/bin/java)
export PATH=$JAVA_HOME/jre/bin:$PATH
使用带有 a 的变量赋值$()
告诉 Linux 进行命令替换。因此,它尝试运行命令/usr/bin/java
并将输出分配给变量。
你应该使用:
export JAVA_HOME=/usr/bin/java
export PATH=$JAVA_HOME/jre/bin:$PATH
然后,为了使新变量生效,请执行您的配置文件脚本:
. ~/.profile
.
注意开头的额外内容。如果没有它,变量赋值将只在脚本范围内持续存在。