# Why doesnt ^Z work on bash?

## dE_logics

Characters starting with unprintable ASCII characters are handed by the kernel; so ^Z must also be handled by the kernel; finally stty report it.

So why is it that Linux does not stop the bash process when the pts/tty receives a ^Z?

----------

## John R. Graham

Without looking at the source code, I'd assume that Bash registers signal handlers for SIGTSTP (the signal raised when you press Ctrl-Z) and SIGINT (the signal raised when you press Ctrl-C), among others. For more background material, Google "Linux Signals".

- John

----------

## desultory

Confirmed by man 1 bash:

 *Quote:*   

> SIGNALS
> 
>        When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not kill an interactive shell), and SIGINT is caught  and  handled  (so
> 
>        that the wait builtin is interruptible).  In all cases, bash ignores SIGQUIT.  If job control is in effect, bash ignores SIGTTIN, SIGTTOU, and SIGTSTP.
> ...

 

----------

## dE_logics

 *John R. Graham wrote:*   

> Without looking at the source code, I'd assume that Bash registers signal handlers for SIGTSTP (the signal raised when you press Ctrl-Z) and SIGINT (the signal raised when you press Ctrl-C), among others. For more background material, Google "Linux Signals".
> 
> - John

 

Having asked this question you must have realized very well that I know what are signs, what is a terminal and what is bash and how they work.

Now I just need clarification based on what I understand.

----------

## dE_logics

 *desultory wrote:*   

> Confirmed by man 1 bash:
> 
>  *Quote:*   SIGNALS
> 
>        When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not kill an interactive shell), and SIGINT is caught  and  handled  (so
> ...

 

SIGSTOP Is not mentioned here. Besides it can't be handled. That's the reason why I didn't bother with bash.

Thanks for any response in this un-explored topic.

----------

## John R. Graham

 *dE_logics wrote:*   

> Having asked this question you must have realized very well that I know what are signs, what is a terminal and what is bash and how they work.

 Sorry; no slight intended. However, from the way you phrased your question, it wasn't obvious to me that you knew those things.

So what is it you don't understand?

- John

----------

## dE_logics

 *John R. Graham wrote:*   

>  *dE_logics wrote:*   Having asked this question you must have realized very well that I know what are signs, what is a terminal and what is bash and how they work. Sorry; no slight intended. However, from the way you phrased your question, it wasn't obvious to me that you knew those things.
> 
> So what is it you don't understand?
> 
> - John

 

Thanks for the interest.

Since sending signals based on character/character sequence encountered on the (p)tty is the job of the kernel, why doesn't it stop bash when it encounters ctrl+z?

----------

## John R. Graham

A registered signal handler can handle a signal by "doing nothing". Bash registers such a "do nothing" signal handler for SIGTSTP. All of the behavior you normally see when you press Ctrl-Z within an app is from the default SIGTSTP signal handler, which does not "do nothing". So Bash does receive the SIGTSTP signal, but you don't notice because the "do nothing" signal handler effectively causes the signal to be ignored.

- John

----------

## dE_logics

 *dE_logics wrote:*   

>  *desultory wrote:*   Confirmed by man 1 bash:
> 
>  *Quote:*   SIGNALS
> 
>        When bash is interactive, in the absence of any traps, it ignores SIGTERM (so that kill 0 does not kill an interactive shell), and SIGINT is caught  and  handled  (so
> ...

 

 *Quote:*   

> The SIGSTOP signal stops the process. It cannot be handled, ignored, or blocked. 

 

http://www.gnu.org/s/libc/manual/html_node/Job-Control-Signals.html

----------

## russK

 *dE_logics wrote:*   

> Characters starting with unprintable ASCII characters are handed by the kernel; so ^Z must also be handled by the kernel; finally stty report it.
> 
> So why is it that Linux does not stop the bash process when the pts/tty receives a ^Z?

 

By the way, don't confuse ^Z with the STOP signal itself.  There is really nothing that connects the ^Z to the STOP signal other than convention.  As you know, bash, like many other shells, allows you to have multiple "jobs" either in the foreground or the background.  Also bash provides job control commands I am sure you have read about.

Also, I don't believe the kernel necessarily handles the ^Z per se other than providing for the I/O.  I would suggest the ^Z is really handled by bash and the terminal, and when it sees the ^Z, it will send a STOP signal a foreground process.  Note, the ^Z is typically the suspend character, but you can change it with 'stty'.

Here, literally use the caret "^" and capital "X" together.  This will change your suspend character from ^Z to ^X for your current shell.

```
stty susp "^X"
```

----------

## desultory

 *dE_logics wrote:*   

>  *Quote:*   The SIGSTOP signal stops the process. It cannot be handled, ignored, or blocked.  
> 
> http://www.gnu.org/s/libc/manual/html_node/Job-Control-Signals.html

 

You might want to review the very next entry on that same page, especially the final paragraph thereof:

 *Quote:*   

> Macro: int SIGTSTP
> 
>     The SIGTSTP signal is an interactive stop signal. Unlike SIGSTOP, this signal can be handled and ignored.
> 
>     Your program should handle this signal if you have a special need to leave files or system tables in a secure state when a process is stopped. For example, programs that turn off echoing should handle SIGTSTP so they can turn echoing back on before stopping.
> ...

 

----------

## dE_logics

 *desultory wrote:*   

>  *dE_logics wrote:*    *Quote:*   The SIGSTOP signal stops the process. It cannot be handled, ignored, or blocked.  
> 
> http://www.gnu.org/s/libc/manual/html_node/Job-Control-Signals.html 
> 
> You might want to review the very next entry on that same page, especially the final paragraph thereof:
> ...

 

Got it. Thanks a lot everyone.

So the kernel sends SIGSTP instead of SIGSTOP.

----------

## John R. Graham

Yes, but you have a small typo above. It's SIGTSTP that's raised when you type Ctrl-Z.  :Wink: 

- John

----------

## russK

By the way, if you are using bash and you have a process running in the foreground, ^Z will stop the process, but it's bash that sends the SIGSTP, not the kernel.

----------

