简体   繁体   English

Ubuntu使用脚本将JAVA_HOME路径添加到bashrc不起作用

[英]Ubuntu using script to add JAVA_HOME path to bashrc not working

I am trying to write a script which will add the JAVA_HOME path to bashrc. 我正在尝试编写一个脚本,该脚本会将JAVA_HOME路径添加到bashrc。 But I keep getting the wrong output. 但是我一直得到错误的输出。

Using the script below: 使用以下脚本:

echo "export JAVA_HOME=/usr/lib/jvm/java-7-oracle" >> ~/.bashrc
echo "export PATH=$PATH:$JAVA_HOME/bin" >> ~/.bashrc

I get the output below in bashrc 我在bashrc中得到以下输出

export JAVA_HOME=/usr/lib/jvm/java-7-oracle
export PATH=/home/ubuntu/apache-maven-3.3.9/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/usr$

The desired output is export PATH=$PATH:$JAVA_HOME/bin for the path. 所需的输出是export PATH=$PATH:$JAVA_HOME/bin

This script is not just intended for adding java path, I wan't to add path for hadoop, spark and hbase. 该脚本不仅用于添加Java路径,也不会为hadoop,spark和hbase添加路径。 I get same output for each of those. 我得到每个相同的输出。 Any help would be appreciated. 任何帮助,将不胜感激。

What is happening is that $PATH and $JAVA_HOME are being expanded before the line is added to .bashrc . 发生的是,在将行添加到.bashrc之前,已扩展$ PATH和$ JAVA_HOME。

They need to be escaped; 他们需要逃脱; eg 例如

echo "export PATH=\$PATH:\$JAVA_HOME/bin" >> ~/.bashrc

or 要么

echo 'export PATH=$PATH:$JAVA_HOME/bin' >> ~/.bashrc

However, you want to be really careful with this kind of "brute-force" editing of shell "rc" files. 但是,您要对外壳“ rc”文件的这种“强力”编辑非常小心。 It can be dangerous. 可能很危险。

A better idea would be to either do the changes by hand, or put the settings into the respective wrapper scripts that launch your Java-based applications 一个更好的主意是手动进行更改,或者将设置放入启动基于Java的应用程序的相应包装脚本中

echo只是输出命令文本,摆脱回声,然后实际执行命令。

The issue is that you are using double quotes. 问题是您正在使用双引号。 These evaluate the variables (in this case $JAVA_HOME , which isn't set yet). 它们计算变量(在本例中$JAVA_HOME ,尚未设置)。 If you use single quotes instead, it will output the literal instead of the evaluated. 如果您使用单引号代替,它将输出文字而不是被评估。 eg 例如

echo 'export JAVA_HOME=/usr/lib/jvm/java-7-oracle' >> ~/.bashrc echo 'export PATH=$PATH:$JAVA_HOME/bin' >> ~/.bashrc

See Difference between single and double quotes in Bash for more information. 有关更多信息,请参见Bash中单引号和双引号之间的差异

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM