Search code examples
ubuntufish

Added function parameters to function


I have a function called auth and I want to have function parameters but I have no idea how to do that. The function parameters being username, and repo. I tried to do it the bash style but it didn't work, searching online didn't help much either. This is what I currently have.

function auth 
    username = $1
    repo = $2
    string = "[email protected]:${username}/${repo}"
    git remote set-url $string
end

I've also tried

function auth 
    $username = $1
    $repo = $2
    $string = "[email protected]:$username/$repo"
    git remote set-url {$string}
end

But it didn't work either. The error occurs where I set the variables username, string, and repo

~/.config/fish/functions/auth.fish (line 2): The expanded command was empty.
    $username = $1
    ^
in function 'auth' with arguments '1 2'
~/.config/fish/functions/auth.fish (line 3): The expanded command was empty.
    $repo = $2
    ^
in function 'auth' with arguments '1 2'
~/.config/fish/functions/auth.fish (line 5): The expanded command was empty.
    $string = "[email protected]:$username/$repo"
    ^

Solution

  • Fish stores its arguments in a list called "$argv", so you want to use that.

    Also $var = value is wrong syntax in both fish and bash. In bash it's

    var=value
    

    (without the $ and without the spaces around =).

    In fish it's

    set var value
    

    (also without a $).

    So what you want is

    function auth 
        set username $argv[1]
        set repo $argv[2]
        set string "[email protected]:$username/$repo"
        git remote set-url $string
    end
    

    But really, you want to read the documentation, specifically the section on $argv and the tutorial. This should also be accessible by simply running help in fish, which should open a local copy in your browser.