Question

I'm trying to convert my bashrc configuration into the fish shell config, and I have this part in the prompt:

function fish_prompt
  ....

  if [ $RETVAL -ne 0 ]
    printf "✘"
  else
    printf "✓"
  end
  ....
end

This config leads to an error:

test: Missing argument at index 2

Can anybody explain to me, what's wrong here?

Was it helpful?

Solution

RETVAL is unset:

$ set -e RETVAL
$ if [ $RETVAL -ne 0 ]; echo foo; end
test: Missing argument at index 2
$ set RETVAL 1
$ if [ $RETVAL -ne 0 ]; echo foo; end
foo

If you want to suppress the error message, test for the existence of the variable. Also, use fish's builtin test instead of the external [

$ set -e RETVAL
$ if begin; set -q RETVAL; and test $RETVAL -ne 0; end; echo foo; else; echo bar; end
bar
$ set RETVAL 1
$ if begin; set -q RETVAL; and test $RETVAL -ne 0; end; echo foo; else; echo bar; end
foo

Actually, this is probably tidier:

if test -n "$RETVAL" -a "$RETVAL" -ne 0; echo foo; end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top