0

I would like convert a Bash script to a Fish script to test if file .nvmrc exists.

Bash script:

## Auto load nvm when there's a .nvmrc file
OLD_PWD=""
promptCommand() {
    if [ "$OLD_PWD" != "$PWD" ] ;
        then
        OLD_PWD="$PWD"
        if [ -e .nvmrc ] ;
            then nvm use;
        fi
    fi
}
export PROMPT_COMMAND=promptCommand

And Fish script (don't work):

set OLD_PWD ""
function nvm_prompt
    if [ "$OLD_PWD" != "$PWD" ]
        then
        OLD_PWD="$PWD"
        if [ -e .nvmrc ]
            then bass source ~/.nvm/nvm.sh --no-use ';' nvm use
        end
    end
end
s-leg3ndz
  • 3,260
  • 10
  • 32
  • 60
  • Remember that `[` is essentially an alias for the `test` command, so you need spaces around the brackets, just like bash. – glenn jackman Jan 08 '18 at 14:52
  • More importantly, the PROMPT_COMMAND variable does nothing in fish. See https://fishshell.com/docs/current/tutorial.html#tut_prompt – glenn jackman Jan 08 '18 at 14:53
  • Also have a look at [this](https://stackoverflow.com/a/29671880/7575111). – NullDev Jan 08 '18 at 14:54
  • Hi everybody, i've edit my first post with your comment, but no result ^ – s-leg3ndz Jan 08 '18 at 16:42
  • This reads much like a "please do my work for me" question. If what you want to know is how to perform a specific operation in fish, isolate that operation and ask about that alone -- but it should be a question *about fish*; there's no good justification for having a bash tag on a question where only someone with expertise in fish can answer it. – Charles Duffy Jan 08 '18 at 16:50
  • Beyond that -- instead of "doesn't work", describe *exactly what* doesn't work in the question itself; that means including an exact error message -- see the documentation on building a [mcve]. If you only need the single `bass source` line to create your error, for example, then only that one line should be in the question; by contrast, if you *don't* need that line to reproduce the problem, it shouldn't be in the question at all. – Charles Duffy Jan 08 '18 at 16:54

1 Answers1

3

First of all, fish's if does not use the word then. It's just gone.

So

if [ "$OLD_PWD" != "$PWD" ]
    then

becomes just

if [ "$OLD_PWD" != "$PWD" ]

(and similarly with the other if)

Secondly,

OLD_PWD="$PWD"

isn't valid fish script (as it'll have told you). Use

set -g OLD_PWD "$PWD"

Thirdly, as it stands, this function is currently defined but never run. You need some way to execute it when the PWD changes. And, as luck would have it, fish has a way to define functions to run on variable changes - the --on-variable VARNAME option to function.

So your solution would look something like this:

function nvm_prompt --on-variable PWD
    if [ "$OLD_PWD" != "$PWD" ] 
        set -g OLD_PWD "$PWD"
        if [ -e .nvmrc ]
            bass source ~/.nvm/nvm.sh --no-use ';' nvm use
        end
    end
end

You might even do away with the $OLD_PWD check, or you might not, since the event is also triggered when you do e.g. cd . (i.e. when the variable is set again to the same value).

Also, I'm assuming the name means that it is run when the prompt is displayed, not that it actually displays anything by itself - in which case you'd stick it in your fish_prompt function (try funced fish_prompt and funcsave fish_prompt).

faho
  • 14,470
  • 2
  • 37
  • 47
  • Thank you for your good explanation ! I've add edit function with `--on-variable` and it work very nice :) `function nvm_prompt --on-variable PWD` – s-leg3ndz Jan 09 '18 at 08:14