5

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.

5
  • The file ~/.config/fish/fish_variables has no intrinsic meaning to fish. That is, fish won't automatically source the contents of that file. Commented 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
    Commented 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"? Commented 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
    Commented Feb 3, 2019 at 23:37
  • 1
    @kmb You should mark Jake's answer as solving your problem. Commented Feb 4, 2019 at 2:01

2 Answers 2

7

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)
3
  • Unfortunately, parenthesis are also not working.
    – kmb
    Commented 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? Commented 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
    Commented Feb 3, 2019 at 22:42
-1
function javaenv
   if test (count $argv) -eq 0
       /usr/libexec/java_home -V
   else
       switch $argv[1]
       case 'ls'
           /usr/libexec/java_home -V
       case 'set'
           set -xU JAVA_HOME (/usr/libexec/java_home -v $argv[2])
       end
   end
end

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

javaenv

and set the contents of java_home with

javaenv set 1.8

Idea and contents taken from here

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .