.bash_profile: line 260: syntax error near unexpected token `('

-2

I have bash function:

WORKING_REACT_EXAMPLES="$HOME/tutorials/javascript/react/working-react-examples"
dualpush() { cd $WORKING_REACT_EXAMPLES; cmpushall "$1"; cd ..; cmpushall $1; cd $WORKING_REACT_EXAMPLES ;}

it looks similar to my other functions. When I try to source .bashrc I get

$ src
-bash: /Users/cchilders/.bash_profile: line 260: syntax error near unexpected token `('
-bash: /Users/cchilders/.bash_profile: line 260: `dualpush() { cd $WORKING_REACT_EXAMPLES; cmpushall "$1"; cd ..; cmpushall "$1"; cd $WORKING_REACT_EXAMPLES ;}'

Shellcheck warns to double quote something, yet doesn't say any errors, so I do

dualpush() { cd "$WORKING_REACT_EXAMPLES"; cmpushall "$1"; cd ..; cmpushall "$1"; cd "$WORKING_REACT_EXAMPLES" ;}

and get same error

What is causing the syntax error? Thank you

codyc4321

Posted 2017-01-31T06:58:22.393

Reputation: 298

3The error message says "line 260". You only showed two lines. What does the rest of the file look like? – user1686 – 2017-01-31T07:17:57.740

the rest of the file isn't relevant to the problem, this is line 260. commenting this line out fixes the proble, so the issue is in this line. I added it all anyway – codyc4321 – 2017-01-31T14:26:58.653

2Seemingly bogus syntax errors are often caused by subtle syntax errors that precede them. There may be quite some distance between the point the parser gave up and the actual cause. – Daniel B – 2017-01-31T15:03:59.623

that's true, bash is tricky like that – codyc4321 – 2017-01-31T17:04:08.513

Answers

1

This usually happens when the function name is already defined as an alias with parameters.

Alias substitution happens quite early in shell parsing, so it also affects function declarations (as well as other things). For example, if you had the following...

alias dualpush="push; push --again"

dualpush() { push; push --again; }

...then it would be expanded to:

push; push --again() { push; push --again; }

If the original alias ends with a command without parameters, things still work but the function is defined under the wrong name (among other potential surprises!). If, however, the last command in that alias has some arguments, then the expansion is no longer recognized as a function declaration – instead the () appears in the middle of a command-line argument and is rejected by the parser.

user1686

Posted 2017-01-31T06:58:22.393

Reputation: 283 655

correct; I had defined the alias first, and probably sourced. After deciding it required bash function, that alias was stuck in my terminal. so this issue is happening because I'm writing my bash stuff at work when I'm not tired, not due to macs. Ty – codyc4321 – 2017-01-31T17:03:23.457

0

After closing the terminal, the exact same command works. Mac has flaws when using bash functions, and after sourcing bashrc with dualpush commented out, it remains in terminal. Restarting entire terminal, the same command works. I have no understanding of this poor behavior, but this doesn't happen to me on ubuntu

codyc4321

Posted 2017-01-31T06:58:22.393

Reputation: 298

1“after sourcing bashrc with dualpush commented out, it remains in terminal.” – Of course. This is expected behavior. You don’t delete it, after all. You don’t redefine it either, so it stays as is. – Daniel B – 2017-01-31T15:02:00.690

I must have not noticed it before. Sourcing only adds; that makes sense since the terminal can't delete things just because they aren't in bashrc; ty – codyc4321 – 2017-01-31T15:53:58.003

what surprised me is that even if I keep the same the same, it finds an old error and won't let me source. in ubuntu if I source, I get the new, correct command – codyc4321 – 2017-01-31T15:54:31.987