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.

  • 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 Mar 25 '15 at 21:35
  • You should probably show people the immediately preceding lines, too. – JdeBP Mar 25 '15 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 Mar 25 '15 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 Mar 25 '15 at 21:57
  • Add set -x to see what exactly is executed. – michas Mar 25 '15 at 22:09

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.

| improve this answer | |
  • 2
    I added an edit the answer of what I think may have been the issue. – aug Mar 25 '15 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 Mar 26 '15 at 1:01

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.