0

I've been reading various posts about handling SIGINT in bash, but I still don't properly understand it.

I know that trap '_handler_name' SIGINT runs _handler_name when the signal is received. (People always seem to put it in single quotes, but I don't know why. It doesn't seem necessary to me.)

I was hoping to be able to trap SIGINT and handle it without aborting a loop in my script, but that only seems to work when the loop is in its own subshell. (I don't know why that is...)

I had thought that using trap -- '_handler_name' SIGINT might somehow stop other parts of the script from aborting when the signal is received. (This is based upon my reading of this answer.)

So my main question is: what effect does the -- have on trap. I thought that always just meant "that's the end of the switches" but the example I was looking at didn't have a - after that, so it looks redundant.

And sub-questions that would help my understanding are: what effect does trap have on subshells? and why do people put the handler name in quotes in the trap command?

For context, what I'm trying to do is spot a SIGINT, politely kill a couple of processes, then wait for a few seconds for everything to finish before exiting manually.

PS This article was interesting, though I didn't manage to get my solution from reading it.


UPDATE: I've moved what was here to a new question, since it turns out that what I'm asking here isn't the cause of the issue I've observed.

IpsRich
  • 797
  • 10
  • 23
  • There are many question in your post. It's better to make the post concise, limited to one single short problem./ `I don't know why that is` Create a separate question, present a full [MCVE], present the input that you are getting, the input that you expected to get. – KamilCuk Dec 17 '21 at 12:57
  • `I would expect the same output as in the first case.` You changed the condition, `read line` fails, either reads 0 or receives `SIGHUP` - the other side gets closed. – KamilCuk Jan 04 '22 at 10:56

1 Answers1

0

what effect does the -- have on trap

Yes, it just means "that's the end of the switches". https://github.com/bminor/bash/blob/master/builtins/trap.def#L110 -> https://github.com/bminor/bash/blob/f3a35a2d601a55f337f8ca02a541f8c033682247/builtins/bashgetopt.c#L85

what effect does trap have on subshells?

From bash manual command execution environment https://www.gnu.org/software/bash/manual/bash.html#Command-Execution-Environment :

When a simple command other than a builtin or shell function is to be executed, it is invoked in a separate execution environment that consists of the following. Unless otherwise noted, the values are inherited from the shell.

...

  • traps caught by the shell are reset to the values inherited from the shell’s parent, and traps ignored by the shell are ignored

...

Command substitution, commands grouped with parentheses, and asynchronous commands are invoked in a subshell environment that is a duplicate of the shell environment, except that traps caught by the shell are reset to the values that the shell inherited from its parent at invocation.

Also below near trap builtin:

Trapped signals that are not being ignored are reset to their original values in a subshell or subshell environment when one is created.

Also errexit is relevant specifically for ERR.

why do people put the handler name in quotes in the trap command?

Cosmetics. Because trap re-evals the string, it's a visual cue that it's a string to be re-evaled, not list of words.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Since this answers my title question, I think it's only right to mark it as the solution - thanks! I'll add a new question to address what it turns out I actually need to know... – IpsRich Jan 04 '22 at 10:41