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?


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. Feb 3, 2019 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, 2019 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"? Feb 3, 2019 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, 2019 at 23:37
  • 1
    @kmb You should mark Jake's answer as solving your problem. Feb 4, 2019 at 2:01

2 Answers 2


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

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, 2019 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? Feb 3, 2019 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, 2019 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? Feb 4, 2019 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, 2019 at 10:54
function javaenv
   if test (count $argv) -eq 0
       /usr/libexec/java_home -V
       switch $argv[1]
       case 'ls'
           /usr/libexec/java_home -V
       case 'set'
           set -xU JAVA_HOME (/usr/libexec/java_home -v $argv[2])

Can put the function above in ~/.config/fish/functions and use it to get the java_home contents with


and set the contents of java_home with

javaenv set 1.8

Idea and contents taken from here


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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