Same Bash works differently on two different machines?

I use Debian on most of my machines, and they all have Debian 12/stable installed on them. I have a set of scripts in a git repo that are sourced by ~/.bashrc (including on a couple Fedora machines). I'm not claiming these scripts are bug-free, but they work pretty well and haven't been significantly changed in several months. A couple days ago I discovered that one of my Debian machines was using a local-only ~/.bashrc, so I linked in this set of scripts which works fine on all the other machines. And got a syntax error, for a script line that works on all other Debian 12 machines, but not this one. I tried the single line of code on the command line and found it errors out there as well: $ gr() { grep --color=auto "$@" ; } bash: syntax error near unexpected token `(' On any other Debian machine, if I run that on the command line, it returns silently and without error. The following output is the same on both the test machines: $ echo $BASH_VERSION 5.2.15(1)-release $ echo $BASH /bin/bash I've run `set | less` and compared the $BASH* variables, and I don't see anything significant. Any advice would be greatly appreciated, as this isn't making any sense to me ... -- Giles https://www.gilesorr.com/ gilesorr@gmail.com

On Mon, 17 Mar 2025 at 10:16, D. Hugh Redelmeier via talk <talk@gtalug.org> wrote:
From: Giles Orr via talk <talk@gtalug.org>
$ gr() { grep --color=auto "$@" ; } bash: syntax error near unexpected token `('
Does this tell you anything interesting?
$ type 'gr' $ type gr
Does it ever. On all the other machines: gr is a function gr () { grep --color=auto "$@" } But on the machine in question: $ type gr gr is aliased to `grep --color=auto ' (Same answer whether I ask `type 'gr'` or `type gr`.) I was initially unable to determine where that alias was being set, but when I did `unalias gr` I could then set the "gr" function without Bash complaining, so that's clearly the problem! (The alias was being set by a very old file in /etc/ ...) Thank you so much. -- Giles https://www.gilesorr.com/ gilesorr@gmail.com

Reply is below quoted content. On Monday, March 17, 2025 11:09:43 a.m. Eastern Daylight Saving Time Giles Orr via talk wrote:
On Mon, 17 Mar 2025 at 10:16, D. Hugh Redelmeier via talk
<talk@gtalug.org> wrote:
From: Giles Orr via talk <talk@gtalug.org>
$ gr() { grep --color=auto "$@" ; } bash: syntax error near unexpected token `('
Does this tell you anything interesting?
$ type 'gr' $ type gr
Does it ever. On all the other machines:
gr is a function gr () { grep --color=auto "$@" }
But on the machine in question:
$ type gr gr is aliased to `grep --color=auto '
(Same answer whether I ask `type 'gr'` or `type gr`.)
I was initially unable to determine where that alias was being set, but when I did `unalias gr` I could then set the "gr" function without Bash complaining, so that's clearly the problem! (The alias was being set by a very old file in /etc/ ...) Thank you so much.
I think it is because bash will expand an alias if and only if it is at the beginning of a statement. If you define the function as `function gr () { ... } `, it should not experience the same problem. Best regards, tusooa

On Wed, 19 Mar 2025 at 00:35, tusooa via talk <talk@gtalug.org> wrote:
Reply is below quoted content.
On Monday, March 17, 2025 11:09:43 a.m. Eastern Daylight Saving Time Giles Orr via talk wrote:
On Mon, 17 Mar 2025 at 10:16, D. Hugh Redelmeier via talk
<talk@gtalug.org> wrote:
From: Giles Orr via talk <talk@gtalug.org>
$ gr() { grep --color=auto "$@" ; } bash: syntax error near unexpected token `('
Does this tell you anything interesting?
$ type 'gr' $ type gr
Does it ever. On all the other machines:
gr is a function gr () { grep --color=auto "$@" }
But on the machine in question:
$ type gr gr is aliased to `grep --color=auto '
(Same answer whether I ask `type 'gr'` or `type gr`.)
I was initially unable to determine where that alias was being set, but when I did `unalias gr` I could then set the "gr" function without Bash complaining, so that's clearly the problem! (The alias was being set by a very old file in /etc/ ...) Thank you so much.
I think it is because bash will expand an alias if and only if it is at the beginning of a statement. If you define the function as `function gr () { ... } `, it should not experience the same problem.
Best regards, tusooa
Let's test ... $ type gr gr is a function ... $ unset gr $ type gr bash: type: gr: not found Now I can set the alias, and then try creating a function over top of it ...: $ alias gr="grep --color=auto " $ type gr gr is aliased to `grep --color=auto ' $ function gr () { grep --color=auto "$@" ; } $ It returned silently. Perhaps you were correct?: $ type gr gr is aliased to `grep --color=auto ' So ... you were sort of correct. Saying 'function' first prevented the violent allergic response and my start-up scripts would have proceeded fine (because that step didn't error out), but the final result would still have been unexpected: my start-up scripts would have appeared to set a gr() function, but I would still have had the previously set 'gr' alias. I'm not sure if it's better in general to start with the 'function' builtin keyword, but in this case ... I would rather have seen the error as I did so I could track down and fix the error. -- Giles https://www.gilesorr.com/ gilesorr@gmail.com
participants (3)
-
D. Hugh Redelmeier
-
Giles Orr
-
tusooa