Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying out the fish shell 2.0 to replace bash. I'm trying to convert the following bash function to a fish function, but its giving me hell. I have a several similar functions so if someone could point me in the right direction with an example I would be very grateful

connect() {
    if [ "$2" != "" ]
            ssh -ti ~/.ssh/work/id_rsa user@host$1.domain.com "sudo ssh $2"
    if [ "$2" == "" ]
            ssh -ti ~/.ssh/work/id_rsa user@host$1.domain.com "sudo -i"

Tried just replacing $1 and $2 with $argv[1] and $argv[2] but it complains about not being able to locate the end of block. I know its because the scripting synxtax is quite different in fish. I tried starting from a simpler function and had troubles with figuring out how to get it to do one thing if 1 variable is passed, and another thing if 2 variables are passed:

function testvarz
    switch "$argv[2]";
            case ""
                    echo "no second variable passed"
            case != ""
                    echo "second variable passed"


but that doesn't work, if you pass 2 variable it does nothing, if you pass 1 it complains about

share|improve this question

migrated from serverfault.com Jun 8 '13 at 0:45

This question came from our site for system and network administrators.

up vote 4 down vote accepted

Here is a working function that's similar to your test function.

 function testvarz
      switch (count $argv)
          case 0
              echo 'zero'
          case 1
              echo "one $argv[1]"
          case 2
              echo "two $argv[1] $argv[2]"
          case '*'
              echo 'else'

You can use a similar structure for your connect function.

By the way, you should similarly use $# in your Bash function instead of trying to rely on $2 being null. You can also use a case statement in Bash.

share|improve this answer
Thanks for the example and tips – MrSilver AG Jun 8 '13 at 2:09

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.