Jelmergu
Jelmergu

Reputation: 964

bash seeing parameter as function

I have this function to make make it simpeler for me to push to git. It works fine with pushing without arguments, however when I pass a parameter to the function I get -bash: parameter: command not found. For as far as I know, I am calling the function correctly.

function gitPush {
  if ($1)
    then
    if ($2)
     then
     git push $2 $1
    else
     git push origin $1
    fi
  else
    git push origin master
  fi
}

When I call it using gitPush it works fine, however gitPush branch does not work.

I would like to know what I am doing wrong and how to fix it.

I don't know if it affects the execution of the function (expect not), but I am using Putty

Upvotes: 1

Views: 70

Answers (2)

marcolz
marcolz

Reputation: 2970

I guess you might do this quite a lot shorter with:

function gitPush {
    local remote=${2:-origin}
    local branch=${1:-master}
    git push "${remote}" "${branch}"
}

But all-in-all this is probably not something you would want to use anyway.

in your .gitconfig you could add:

[push]
default = tracking

That way wou will always push to the tracked remote of the current branch if you do not supply arguments.

Upvotes: 3

Tom Fenech
Tom Fenech

Reputation: 74695

($1) isn't doing what you think it does - it is attempting to execute the command stored in the first argument, in a subshell.

To test for the existence of an argument, you can use the following:

if [[ -n $1 ]]

Or to simply count the arguments, you can use the following:

if [[ $# -eq 1 ]]

Upvotes: 4

Related Questions