5

I cannot define a bash function only for specific names and when I use <function name>() syntax and when I try to define it in the current shell (i.e. not in a subshell).

$ cat -n test.sh 
1   function f { true; }
2   
3   f() { true; }
4   
5   function make { true; }
6   
7   make() { true; }

$ function f { true; } && f() { true; } #OK

$ function make { true; } && make() { true; } #NG
bash: syntax error near unexpected token `(`

$ bash test.sh #OK

$ source test.sh #NG
bash: test.sh: line 7: syntax error near unexpected token `(`
bash: test.sh: line 7: `make() { true; }'

What's happening here? Is this an expected behavior? I believe, at least, this is not syntax error near unexpected token `(' as the error message suggests.


Environment

$ bash --version
GNU bash, version 5.0.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
4
  • It shouldn't matter. You can overwrite a system name. Your syntax looks fine. I'm still looking at it. I can define function make { echo bar; }; make() { echo foo; }; make --version at the command line and as expected, get "foo" back, so I'm a bit confused as to your error. Same GNU bash, version 5.0.16(1)-release (x86_64-pc-linux-gnu) Commented May 5, 2020 at 1:09
  • 1
    @DavidC.Rankin I have yeas of experience on bash, but haven't encountered this problem until today... Commented May 5, 2020 at 1:11
  • @JohnKugelman Couldn't reproduce on GNU bash, version 5.0.3(1)-release (arm-unknown-linux-gnueabihf) on Raspberry Pi 3. So maybe the problem is due to my configuration but is it possible to customize bash not to accept specific names in the first place? I haven't modified the source code of bash. Commented May 5, 2020 at 1:23
  • I just went through man bash with source as the search term, I can't find anything there that explains it either. Commented May 5, 2020 at 1:23

2 Answers 2

6

You have some sort of make alias that's getting triggered. I can reproduce this if I create an alias with purposeful syntax errors:

$ alias make='@)$*)@'
$ make() { true; }
bash: syntax error near unexpected token `)'

Aliases only execute interactively. They're not active inside scripts, which explains why this only happens when you run the command by hand or with source.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I set alias make='make --no-builtin-rules' but didn't know it is expanded even in the context of function definitions. Thank you very much.
I didn't know it either, and didn't expect it. I would have thought the parentheses would inhibit alias expansion, but nope!
0

The cause of the problem is described in John's answer. I'm writing this answer to give solutions how to avoid the problem.

Solution 1

First undefine the alias and then define the function. Writing like this every time (for safety) seems troublesome, but this is a POSIX-compliant way.

unalias make
make() { true; }

Solution 2

Or use another form of function definitions. This is simple but not POSIX-compliant.

function make { true; }
#or
function make() { true; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.