4

I'm not entirely sure why I'm getting the error in my .bash_profile

syntax error near unexpected token `('

when I use the keyword grom() for my function. I wanted to create a bash function that will just automatically rebase my master branch with origin

# git status
alias gs='git status'

# git stash list
alias gsl='git stash list'  

grom() {
  branch_name="$(git symbolic-ref --short -q HEAD)"
  git fetch && git rebase origin/master && git checkout master && git rebase origin/master && git checkout $branch_name
}

# git stash apply. must append stash@{num}
alias gsa="git stash apply"

When I change the name of the function, it compiles fine. I couldn't find grom as a keyword so I'm not sure what the issue is. If I rename the function to anything else like git-rom or even something like groms, it compiles fine. Is there some special keywords that do not work? This is on Mac OS X.

8
  • 3
    If you're using bash, you may have better luck declaring it as function grom() { … }. Another possibility is that there is an open-parenthesis in the output of your first git command; try git checkout "$branch_name" as your last chained command, which may get you a git error, but at least you'd know what needs fixing. (Sorry, I'm not yet a git guru)
    – Adam Katz
    Commented Mar 25, 2015 at 21:35
  • You should probably show people the immediately preceding lines, too.
    – JdeBP
    Commented Mar 25, 2015 at 21:38
  • @AdamKatz Adding function in front seemed to fix the issue. I tried the latter option and I still got the error. Very intriguing considering I have other functions in my bash-profile that do not require function in front. If you want to add that as an answer, I'd be willing to accept it. I'm still curious why it does this though. Thanks a lot!
    – aug
    Commented Mar 25, 2015 at 21:56
  • @JdeBP I added what was around the function for reference. I do not think the things around it are the issue though considering different names worked like groms() and git-rom().
    – aug
    Commented Mar 25, 2015 at 21:57
  • Add set -x to see what exactly is executed.
    – michas
    Commented Mar 25, 2015 at 22:09

1 Answer 1

7

If you're using bash, you may have better luck declaring it as:

function grom() { … }

(Note: function will not work in strict POSIX shells like dash!)


@aug suggested (via edits to this answer) that this is due to a conflicting alias (or, less plausibly, a builtin that somehow got defined).

The reserved word function either alters the loading order to preempt the collision (aliases expand during function definition) or else avoids the issue by disabling bash's posix mode (which may allow overriding a builtin).

From the bash(1) man page:

Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed.

If you have a conflicting alias, you can try unalias grom before sourcing .bash_profile (it isn't necessary to add to that file unless you're still defining that conflicting alias) to clear your past experiments. Alternatively, launch a new terminal for a clean start.

2
  • 2
    I added an edit the answer of what I think may have been the issue.
    – aug
    Commented Mar 25, 2015 at 23:33
  • 1
    @mikeserv, I've removed the link to your related answer because it's not as obviously relevant and your citation from the man page is preferable. I've also credited you and aug in my answer. Both of you have helped answer why this works and you deserve credit (I've also given your comments +1). Thanks!
    – Adam Katz
    Commented Mar 26, 2015 at 1:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .