2

I just downloaded Java and according to the Java Control Panel the executable is at this directory: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java

Now I want to export an env variable JAVA_HOME as such:

>export JAVA_HOME=“/Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java”

But when I print out the variable everything after the space is butchered

>$JAVA_HOME
-bash: “/Library/Internet: No such file or directory

How do I properly export this path to the variable?

Now there is another question that answers this but that answer there doesn't work:

>SOME_PATH="/mnt/someProject/some path"
>$SOME_PATH
-bash: /mnt/someProject/some: No such file or directory

And using the other answer on there:

>SOME_PATH=/mnt/someProject/some\ path
>$SOME_PATH
-bash: /mnt/someProject/some: No such file or directory

And here is my bash version (I'm on macOS 10.14.5):

>echo $BASH_VERSION
3.2.57(1)-release
etayluz
  • 15,920
  • 23
  • 106
  • 151
  • 1
    Possible duplicate of [How to input a path with a white space?](https://stackoverflow.com/questions/12902227/how-to-input-a-path-with-a-white-space) – vs97 Jul 12 '19 at 14:17
  • I've tried that answer but it doesn't work (see my edit above) – etayluz Jul 12 '19 at 14:22
  • can you try your original statement but without the " " and escaping space with \ ? Third option of the accepted answer of the question to which I linked. – vs97 Jul 12 '19 at 14:23
  • If you are doing cd, use eval: https://stackoverflow.com/questions/589149/bash-script-to-cd-to-directory-with-spaces-in-pathname – B0RDERS Jul 12 '19 at 14:23
  • What will definitely work is `$ export JAVA_HOME="path with spaces"` and then `$ "$JAVA_HOME"` - not sure if you're in a position to do this. – Aleks G Jul 12 '19 at 14:30
  • This is exactly what I have above and it's not working – etayluz Jul 12 '19 at 14:32
  • It looks like it works right if you place "echo" in front of the env name. not sure why not otherwise – etayluz Jul 12 '19 at 15:05

3 Answers3

1

Single quote and double quote , everything works:

[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ echo $BASH_VERSION
4.4.12(1)-release
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ export JAVA_HOME="/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java"
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ echo $JAVA_HOME
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ export JAVA_HOME='/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java'
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ echo $JAVA_HOME
/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java
[iahmad@web-prod-ijaz001 ~]$ 
[iahmad@web-prod-ijaz001 ~]$ 
Ijaz Ahmad
  • 11,198
  • 9
  • 53
  • 73
  • why does it only work when you put 'echo'. Why does it not work right without the echo? – etayluz Jul 12 '19 at 15:05
  • echo is just to see/confirm the value , it is not needed othervise :) , you script/application will pick the varible anyway – Ijaz Ahmad Jul 12 '19 at 15:06
  • yeah - but what is the difference between putting "echo" on the terminal line vs not putting it before the env variable to see its value? Forget the script - just from the terminal – etayluz Jul 12 '19 at 16:34
  • the difference is if you dont put echo on terminal and just write the variable name , shell will not recognise what is that command and throw error – Ijaz Ahmad Jul 12 '19 at 17:22
0

You must use double quote or single quote: the interpreter is seeing “ as any character, thus trying to find a program “/Library/Internet.

export JAVA_HOME='/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/bin/java'

You don't need to escape anything with single quote, apart from single quote and backslash.

Note that when you are using it directly, like $JAVA_HOME foobar.Main, you need to add double quote as well because in that case $JAVA_HOME contains space:

"$JAVA_HOME" foobar.Main

Some terminal might work without double quote, but you should not rely on it.

However, the JAVA_HOME is wrong: it should point to a folder which contains /bin/java:

export JAVA_HOME="/c/Program Files/Java/jdk1.8.0_202"
export PATH="$JAVA_HOME/bin:$PATH"

In that case, you would simply invoke java and your shell would resolve the executable.

NoDataFound
  • 11,381
  • 33
  • 59
  • This is certainly not the right answer. Did you test that? Test what you wrote and then `$JAVA_HOME` – Aleks G Jul 12 '19 at 14:28
  • Explain what don't work, because the other answer was UPVOTED and explain the same. I'm on Windows, not on Mac, and this work although the path is not the same (/c/Program Files/Java/jdk1.8.0_202). And yes, the path should be the stuff before "bin/java", because that what JAVA_HOME should be... – NoDataFound Jul 12 '19 at 23:51
  • And yes, when you have a space in $JAVA_HOME, you must quote it when invoking it. – NoDataFound Jul 12 '19 at 23:52
0

I think the quote mark you used (“) is not the ASCII quote mark (" or ').

Yun Wu
  • 71
  • 1
  • 6