erthalion
erthalion

Reputation: 3244

Test the RETVAL in the fish shell prompt?

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?

Upvotes: 2

Views: 237

Answers (1)

glenn jackman
glenn jackman

Reputation: 246774

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

Upvotes: 2

Related Questions