bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: An alias named `done` breaks for loops


From: Kerin Millar
Subject: Re: An alias named `done` breaks for loops
Date: Sun, 15 Aug 2021 01:05:16 +0100

On Sat, 14 Aug 2021 15:59:38 -0700
George Nachman <gnachman@llamas.org> wrote:

> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64
> OS: darwin20.5.0
> Compiler: gcc
> Compilation CFLAGS: -g -O2 -Wno-parentheses -Wno-format-security
> uname output: Darwin Georges-Mac-Pro.local 20.5.0 Darwin Kernel Version
> 20.5.0: Sat May  8 05:10:33 PDT 2021; root:xnu-7195.121.3~9/RELEASE_X86_64
> x86_64
> Machine Type: x86_64-apple-darwin20.5.0
> 
> Bash Version: 5.1
> Patch Level: 8
> Release Status: release
> 
> Description:
> Defining an alias named `done` breaks parsing a for loop that does not have
> an `in word` clause.
> 
> 
> Repeat-By:
> 
> Run the following script. It fails with this error:
> 
> myscript.bash: line 7: syntax error near unexpected token `done'
> myscript.bash: line 7: `  done'
> 
> 
> #!/bin/bash
> 
> alias done=""
> 
> f() {
>   for var; do
>   done
> }

This does not constitute a valid test case for two reasons. Firstly, aliases 
have no effect in scripts unless the expand_aliases shell option is set. 
Secondly, the syntax is invalid on account of not having specified a list where 
the for keyword expects to find one. In fact, this is the cause of the 
"unexpected token" error that you encountered, rather than the ineffective 
alias. Below is a revision of your test case that demonstrates the point that 
you were trying to make.

#!/bin/bash
shopt -s expand_aliases
alias done=""
f() {
        for var; do
                :
        done
}

Running this script results in the following error.

  line 8: syntax error near unexpected token `}'
  line 8: `}'

Not defining the alias does, indeed, allow for the revised script to execute 
successfully.

-- 
Kerin Millar



reply via email to

[Prev in Thread] Current Thread [Next in Thread]