0

I was using Bash as my default shell on macOS but I decided to switch to Fish.

I am trying to switch between different versions of Java. In bash it was done using the ~/.bash_profile

export JAVA_HOME=`/usr/libexec/java_home -v 1.7`

I have set the equivalent on Fish ~/.config/fish/fish_variables

set -x JAVA_HOME `/usr/libexec/java_home -v 1.7`

Unfortunately, Java version is not being changed. How can I set environment variables (specific version of Java, in particular) using ~/.config/fish/fish_variables?

UPDATE:

According to the FAQ, instead of ~/.config/fish/fish_variables, ~/.config/fish/config.fish should be used. Also I created ~/.config/fish/fish.config instead of ~/.config/fish/config.fish.

  • The file ~/.config/fish/fish_variables has no intrinsic meaning to fish. That is, fish won't automatically source the contents of that file. – Kurtis Rader Feb 3 at 22:28
  • @KurtisRader According to FAQ fishshell.com/docs/2.2/faq.html I also tried to create ~/.config/fish/config.fish, but it did not help. – kmb Feb 3 at 22:45
  • 2
    That FAQ says nothing about a file named fish_variables. The only fish user config file that is sourced automatically by every fish process is ~/.config/fish/config.fish. If you put a echo hello in that file and start a new fish shell do you see the word "hello"? – Kurtis Rader Feb 3 at 22:55
  • @KurtisRader There was a typo on my side... I have created ~/.config/fish/fish.config instead of ~/.config/fish/config.fish. Thanks for your help! – kmb Feb 3 at 23:37
  • 1
    @kmb You should mark Jake's answer as solving your problem. – Kurtis Rader Feb 4 at 2:01
1

While I am not deeply familiar with Fish, based on what I am reading it seems like the issue is with the backticks in your command:

/usr/libexec/java_home -v 1.7

You see that is just like this in Bash:

$(/usr/libexec/java_home -v 1.7)

Keep that in mind and look at this Fish FAQ entry:

How do I run a subcommand? The backtick doesn't work!

fish uses parentheses for subcommands. For example:

for i in (ls)
    echo $i
end

Knowing that the config line should most likely be:

set -x JAVA_HOME (/usr/libexec/java_home -v 1.7)
  • Unfortunately, parenthesis are also not working. – kmb Feb 3 at 21:53
  • @kbm You need to provide more information. Saying it's "not working" isn't helpful. If you type that set -x ... command interactively what happens? – Kurtis Rader Feb 3 at 22:29
  • @KurtisRader set -x JAVA_HOME (/usr/libexec/java_home -v 1.7) entered directly to shell works fine. After this action java version is changed to 1.7. After closing and opening terminal again, java is set back to version 11. I want to change it permanently, but I need to keep different version on java installed. – kmb Feb 3 at 22:42
  • @kmb Well, my command is just a different version of what you added to ~/.config/fish/fish_variables. Did you test this variant with parenthesis when it was placed in ~/.config/fish/fish_variables? – JakeGould Feb 4 at 2:05
  • 1
    @JakeGould Parenthesis instead of backticks was one of two errors - thank you for pointing it out. The second thing that was wrong, was the file used by me for configuration. Instead of ~/.config/fish/fish_variables, ~/.config/fish/config.fish should be used. I have updated question with this information. – kmb Feb 4 at 10:54

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.