nofork comsub in PS1 + ignoreeof

2023-08-19 Thread Grisha Levit
When a nofork command substitution is in PS1 and ignoreeof is enabled,
(sufficiently repeated) EOF doesn't actually cause the shell to exit:

$ PS1='${ :;}$ ' IGNOREEOF=1 bash --norc
$ ^D
Use "exit" to leave the shell.
$ ^D
Use "exit" to leave the shell.
...


using exec to close a fd in a var crashes bash

2023-08-19 Thread jleivent
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -fstack-protector-strong -Wformat
-Werror=format-sec> uname output: Linux lapdog 6.1.0-11-amd64 #1 SMP
PREEMPT_DYNAMIC Debian 6.1.38-> Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.2
Patch Level: 15
Release Status: release

Description:
Using exec to close a file descriptor in a variable crashes
bash.  It doesn't matter how the fd got into the variable, but
the likely usage is with the {varname} form, as illustrated
below.  I think what is happening is that the exec arg is being
parsed as a target to execute instead of a fd redirection,
because when executed in two levels of shell (to allow the
inner one to crash while leaving the outer), it generates the
following error message:
bash: exec: 10: not found
But even that shouldn't cause the shell to terminate if the
target isn't found, so maybe this is two bugs?

Also note that the quoting and {} around foo in the last line
don't impact the bug.

This bug makes it difficult to use {varname} forms for file
descriptors, as it means there is no easy way to close them
before script exit.

Repeat-By:

exec {foo}>/tmp/foo
exec "${foo}"<&-




Re: using exec to close a fd in a var crashes bash

2023-08-19 Thread Greg Wooledge
On Sat, Aug 19, 2023 at 01:37:31PM -0400, jleivent wrote:
> Repeat-By:
> 
>   exec {foo}>/tmp/foo
>   exec "${foo}"<&-

Remove the quotes and the $ and it should work.

exec {foo}<&-

The way you've got it is essentially:

exec "$foo" <&-

This does't make bash "crash".  It's simply exec-ing a nonexistent
program.



Re: using exec to close a fd in a var crashes bash

2023-08-19 Thread Grisha Levit
On Sat, Aug 19, 2023, 16:42 jleivent  wrote:

> bash: exec: 10: not found
> But even that shouldn't cause the shell to terminate if the
> target isn't found, so maybe this is two bugs?
>
> Repeat-By:
>
> exec {foo}>/tmp/foo
> exec "${foo}"<&-
>

Looks like bash doesn't undo redirections if the exec fails -- so the shell
terminates because the redirection closed stdin.

I agree it would probably make sense to undo them if the exec fails and the
shell is not going to exit.

>