Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: bug report: INTERACTIVECOMMENTS option interacts oddly with shell functions in an interactive context



2021-08-30 14:35:56 -0700, Bart Schaefer:
> On Sun, Aug 29, 2021 at 1:58 PM Glyph <glyph@xxxxxxxxxxxxxxxxx> wrote:
> >
> > Comments in sample_function are interpreted normally, but comments *inside* the "bogus" expression will be interpreted as executing in an interactive context
> 
> Hm, well, this is quite obviously intentional based on the code that
> interprets $(...) expressions.  The only way I can think of to change
> it without breaking the behavior of $(...) typed at the actual command
> line, would be for "source" and related operations to put
> INTERACTIVECOMMENTS into the "sticky" emulation context during parsing
> of the file (even if it was off in the parent shell).
> 
> That is, if "source" et al. always behaved as if
> 
> emulate $(emulate) -o interactivecomments -c 'source ...'
> 
> then comments in $(...) and related contexts would be ignored as if
> they were parsed during the "source" command instead of during the
> execution of the substitution.
> 
> But that could have some pretty confusing side-effects for functions
> that want to examine $options (for example).
> 
> Incidentally, you almost certainly want
> 
> setopt LOCALOPTIONS INTERACTIVECOMMENTS
> 
> in any functions where you intend to put comments inside $(...),
> otherwise you will propagate INTERACTIVECOMMENTS into the parent
> shell.


But that used to work OK, up to 5.4.1 at least, and also applies
to echo $(#comment) outside of functions.

~$ echo 'echo `# c`' > a
~$ . ./a
./a:1: bad pattern: #


I would say it's a bug and a regression or at least a drastic
change of behaviour since the documentation seems to be silent
on that aspect.

It used to work OK even for functions, as comments were always
recognised inside command substitution even in interactive
shells with interactive_comments off:

5.1.1% echo 'f() { echo `# c`; }; f' > a
5.1.1% . ./a

5.1.1% f

5.1.1% which f
f () {
        echo `# c`
}
5.1.1% echo `# c`

5.1.1% echo $(# c
cmdsubst> )

5.1.1% [[ -o interactive_comments ]] || echo no
no

Was mentioned again at https://unix.stackexchange.com/questions/666931/command-not-found-in-sourced-multiline-command-with-comments

Comments are still recognised today (5.8) inside process
substitution in interactive shells:

$ cat <( echo test # comment )
test

See how the ")" is still found as "#" is not considered as a
comment by the outer shell, but the comment is still stripped by
the subshell.

$ set -o interactivecomments
$ cat <( echo test # comment  )
cmdsubst> )
test

-- 
Stephane




Messages sorted by: Reverse Date, Date, Thread, Author