Search code examples
if-statementfish

fish: if error: `[: Missing argument at index 2`


I am a first-time Fish user and curently I try to convert my Bash prompt (PS1) to fish_prompt, which I save to ~/.config/fish/functions/fish_prompt.fish.

This is a similar problem as this, however, the solution does not work for me.

I tried to remove the [], use test, however, I have no idea how to compare strings in Fish

if [ $prompt_hostname == 'remote-host' ]
    set userHost remote
else
    set userHost local
end

Solution

  • You need to quote the variable, and replace == with = (as the former is a bashism).

    The issue is that, just like in bash, an empty variable is removed before it reaches the command, so [ (which is just another name for test) gets its arguments like this:

    [ == 'remote-host' ]
    

    which isn't a valid expression.

    So, this needs to be

    if [ "$prompt_hostname" = 'remote-host' ]
    

    or

    if test "$prompt_hostname" = remote-host
    

    (note that quoting the literal string is unnecessary but harmless in both - it changes nothing, because that string does not have any parts that would be expanded - no $, no *, no (), ....)