echo %JAVA_HOME% works in cmd, but not in git bash
Asked Answered
O

1

5

Windows 10 user. I recently installed git for the first time, but ran into a peculiar issue. I was told that I needed to set the JAVA_HOME environment variable if I wanted to use java within git.

I was able to get JAVA_HOME properly changed for command prompt, but I couldn't get the same change to show up in git bash. Typing in echo %JAVA_HOME% would return a proper path in command prompt, but it would return %JAVA_HOME% in git bash.

I went into system settings and changed the system variable so that JAVA_HOME was pointing to my jdk. I updated the PATH variable in both system and environment variables to point to the bin and the jdk. I closed and reopened both git bash and command prompt. I restarted my machine, and even reinstalled both git and my jdk. I took my jdk from Java 10.0.2 down to the latest java 8 (171).

None of it has worked. I've gone through countless SO posts. What am I doing wrong?

Overcapitalize answered 12/8, 2018 at 17:47 Comment(4)
This has nothing to do with git, only bash. Learn shell syntax.Boll
You're correct, and believe me, I will. It was just something I couldn't see as a beginner, but now I know to really watch out for the syntax.Overcapitalize
The shell used by git is a version of the Linux shell and is supported under Cygwin, which provides a Linux-style command line. This is a completely different environment and expects Linux style environment variables.Sheilasheilah
Thanks Jim. I'd heard talk about git and cygwin being compatible, but I didn't know that git was originally a Linux shell. I guess I should make it a point to look into the history of software before trying to work with it. Would you recommend that I work with Cygwin instead?Overcapitalize
O
10

Turns out, (git) BASH TERMINAL uses a completely different syntax to do echo than COMMMANDLINE TERMINAL.

I had been doing echo %JAVA_HOME% when I should have been doing echo $JAVA_HOME.

I couldn't see that I was supposed to be using $ and not % sign because the symbols looked so similar in my font. I only found out after copy/pasting an answer out of frustration into bash and having it magically work when it hadn't worked prior.

But yes, the answer is to use $ and not % because they mean completely different things. Git bash uses $, command prompt uses %. You'd think they'd just use the same symbol to get the job done.

Overcapitalize answered 12/8, 2018 at 17:47 Comment(6)
Git bash is a Windows version of the bash shell that comes from the Unix/Linux world, where indeed environment variables are prefixed with a $. I have no idea why Microsoft decided to use %, just as I don't have an idea why the use \ where Unix used /. There are probably/maybe valid reasons :)Luana
I guess it does make sense then. I wasn't aware of the origins of git bash, so I couldn't see why they would choose to do things differently.Overcapitalize
@Luana MS-DOS didn't have directories, but had options for command line - (like now dir /w) - so when they added directories, they were feqd and had to keep options working by using \ for directories.Joacima
In bash you don't have a trailing $ after the variable nameFugacity
@Fugacity touche. Edited.Overcapitalize
@Luana everything traces back to CP/M, including drive letters. Regarding the % symbol I've asked a question for it hereFugacity

© 2022 - 2024 — McMap. All rights reserved.