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

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

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


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.

  • Note that there is no colon after the cases. – nnnmmm Aug 7 '18 at 9:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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