Does Bash 5.1 block SIGINT until here-document is ready?

2020-12-12 Thread Oğuz
In Bash 5.1, Ctrl-C doesn't interrupt the command below. The only way out I
guess is to stop it Ctrl-Z and run `kill %%`, but that's really too much
work.

cat <

Re: Does Bash 5.1 block SIGINT until here-document is ready?

2020-12-12 Thread Chet Ramey

On 12/12/20 5:54 AM, Oğuz wrote:

In Bash 5.1, Ctrl-C doesn't interrupt the command below. The only way out I
guess is to stop it Ctrl-Z and run `kill %%`, but that's really too much
work.

 cat <

It's not a signal blocking problem. It's a combination of an interactive
shell and the shell forked to run the command substitution inside the shell
forked to run `cat', which is running the redirection. The issue is that
the child running the command subsitition is not placed into the correct
process group, so its process group is not the same as the terminal's. It
should join its parent's process group.

Chet
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/



Re: Bash 5.1 fails parallel builds from source

2020-12-12 Thread Emanuel Haupt
Emanuel Haupt  wrote:
> On 12/7/20 1:44 PM, Fazal Majid wrote:
> >> On Dec 7, 2020, at 15:37, Chet Ramey  wrote:
> >>
> >> Thanks for the report. I've never actually encountered this error.
> >> Just lucky, I guess.
> >
> > It’s a race condition. The machine I run it on has 6 cores and HT,
> > so I run it with a `make -j 12`, and even then, it’s not
> > consistently reproducible.
> 
> Sure. I run make -j 4 at a minimum, make -j 8 sometimes, and I've
> never seen it. Not very reproducible.

Shortly after updating the FreeBSD port I received quite a few reports
about this. Although I wasn't able to reproduce the error myself it
appears that gentoo solved the race condition with this patch:

https://gitweb.gentoo.org/repo/gentoo.git/tree/app-shells/bash/files/bash-5.1-parallel_make.patch?id=4c2ebbf4b8bc660beb98cc2d845c73375d6e4f50

It's been confirmed to have solved the issue:

https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=251755

Emanuel



mysteries with the placeholder variable _

2020-12-12 Thread Léa Gris

# GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
bash -c 'unset _;_=42;echo $_;unset _;: $((_=666));echo "$_"'



666


# ksh version sh (AT&T Research) 93u+ 2012-08-01
ksh -c 'unset _;_=42;echo $_;unset _;: $((_=666));echo "$_"'


42
-3,02546243348e-123


# zsh 5.8 (x86_64-ubuntu-linux-gnu)
zsh -c 'unset _;_=42;echo $_;unset _;: $((_=666));echo "$_"'



666


# dash 0.5.10.2-7
zsh -c 'unset _;_=42;echo $_;unset _;: $((_=666));echo "$_"'


42
666



It raises multiple observations:

- I thought the placeholder variable _ was a sinkhole like /dev/null. It 
seems like it can get assigned values within arithmetic expressions in 
bash, dash and zsh


- The weird working of ksh if out of scope here, look like any number 
not power of 2 produces the strange output that is not even an integer.


- Dash seems to handle _ as a regular variable and accept assignment, 
which is probably conformant to POSIX shell specifications.


--
Léa Gris




Re: mysteries with the placeholder variable _

2020-12-12 Thread Robert Elz
Date:Sat, 12 Dec 2020 23:51:19 +0100
From:=?UTF-8?B?TMOpYSBHcmlz?= 
Message-ID:  

  | - I thought the placeholder variable _

It isn't really a placeholder variable, it is a magic piece of sh*t
invented by ksh (initially) to fill the role that !$ had in csh
(bash has !$ so it isn't needed for that in bash, and $_ is/was a
VERY poor substitute in any case).

Then it was given all kinds of other meanings in other contexts.

The whole thing is a disaster, just do your best to forget it exists,
and *never* use it for anything.

kre




Re: mysteries with the placeholder variable _

2020-12-12 Thread Lawrence Velázquez
> On Dec 12, 2020, at 5:51 PM, Léa Gris  wrote:
> 
> It raises multiple observations:
> 
> - I thought the placeholder variable _ was a sinkhole like /dev/null.

It is not. I expect you've been deceived by countless examples like:

read var1 var2 _ var3 _ var4

Your three non-dash shells explicitly consider $_ to be a special
variable and overwrite it regularly. This may contribute to the
misconception that assigning to it has no effect.

The bash 5.0.17 man page:

_   At shell startup, set to the absolute pathname used to invoke
the shell or shell script being executed as passed in the
environment or argument list. Subsequently, expands to the last
argument to the previous simple command executed in the
foreground, after expansion. Also set to the full pathname used
to invoke each command executed and placed in the environment
exported to that command. When checking mail, this parameter
holds the name of the mail file currently being checked.

The ksh93u+ man page:

_   Initially, the value of _ is an absolute pathname of the shell
or script being executed as passed in the environment.
Subsequently it is assigned the last argument of the previous
command. This parameter is not set for commands which are
asynchronous. This parameter is also used to hold the name of
the matching MAIL file when checking for mail. While defining
a compound variable or a type, _ is initialized as a reference
to the compound variable or type. When a discipline function is
invoked, _ is initialized as a reference to the variable
associated with the call to this function. Finally when _ is
used as the name of the first variable of a type definition, the
new type is derived from the type of the first variable (See
Type Variables below.).

The zshparam(1) man page for zsh 5.8:

_The last argument of the previous command. Also, this
parameter is set in the environment of every command
executed to the full pathname of the command.

As you noticed with ksh, one can't be sure that these shells will
gracefully handle a user writing to $_, which is why Greg's FAQ
discourages doing so (https://mywiki.wooledge.org/BashFAQ/001):

Note that this usage of _ is only guaranteed to work in Bash. Many
other shells use _ for other purposes that will at best cause this
to not have the desired effect, and can break the script entirely.
It is better to choose a unique variable that isn't used elsewhere
in the script, even though _ is a common Bash convention. 


> It seems like it can get assigned values within arithmetic expressions
> in bash, dash and zsh


Be careful how you test this hypothesis. The command

: $((_=666))

might expand to

: 666

so one might expect the non-dash shells to overwrite $_ with "666"
anyway, since that is the last argument of the command. Indeed:

% cat /tmp/underscore_test
unset _
: $((_=666)) 'last arg'
printf '<%s>\n' "$_"

% bash /tmp/underscore_test

% ksh /tmp/underscore_test

% zsh /tmp/underscore_test


One might try ((_=666)), which doesn't expand to "666". Here the
behaviors diverge significantly immediately after the attempted
assignment, but all three shells recover after the subsequent
command:

% cat /tmp/underscore_test
unset _
((_=666))
printf '<%s>\n' "$_"

: hi
printf '<%s>\n' "$_"

% bash /tmp/underscore_test
<666>

% ksh /tmp/underscore_test
<-3.02546243348e-123>

% zsh /tmp/underscore_test
<_>


These might not be perfect experiments (I'm in a bit of a rush),
but the point stands: Writing to $_ is inconsistent and unpredictable
and you shouldn't do it.

> - The weird working of ksh if out of scope here, look like any
> number not power of 2 produces the strange output that is not even
> an integer.


Given ksh's age and expectation that users are not writing to $_,
I wouldn't be surprised if doing so triggers some sort of bug.

vq