0

I'm currently doing some Bash scripting and have built a tool that returns log information. However, the script sometimes returns a lot of lines (depending on what is going on on the network) and Ctrl+S helps to read the printed lines by "freezing" the output. I've read that the Bash pauses flow-control (XOFF) when pressing that key combination until Ctrl+Q is pressed.

Is there a way that I can print a message when pressing Ctrl+S in the Bash before it pauses?

For example, when the user presses Ctrl+S a message like Stopped - press Ctrl+Q to proceed (or whatever) appears before the Bash pauses the output?

5
  • 3
    That has nothing to do with bash (or any shell for that matters). It's the terminal device driver that pauses output when it receives the ^S character from the terminal. Commented Oct 6, 2020 at 7:15
  • Neither bash nor your tool does even know that its output has been stopped. Commented Oct 6, 2020 at 7:25
  • So, if I get this right, there is no way to do what I wanted? Commented Oct 6, 2020 at 8:19
  • Correct. You cannot detect when Ctrl/S is pressed, so you cannot print a message. Is there any reason why you can't pipe your output to a pager (less) or run it inside something with scrollback such as screen or tmux? Commented Oct 6, 2020 at 8:20
  • I guess not. I just have to revise and modify the script then, which should not be a problem at all. Commented Oct 6, 2020 at 8:36

1 Answer 1

1

That behaviour has nothing to do with bash (or any shell for that matters). It's the terminal device driver that pauses output (stops sending data to the terminal) when it receives the ^S character (or whatever is set by stty stop) from the terminal.

Applications started by your shell in that terminal won't see that ^S character even if they read from the terminal device.

The shell is just another application that runs in the terminal and whose job is just to interpret command lines and start the corresponding command in separate processes. While the commands are running, the shell does nothing, it just waits for them to finish so it can prompt you for another command to enter.

Actually, modern shells including bash disable that flow control process when they (their command line editor) interacts with the terminal device. You'll notice that when you press Ctrl+S at the bash prompt, and assuming it's in emacs mode, bash handles it as an incremental search widget invocation.

Here for a message to be issued when you press Ctrl+S, it's the terminal device driver (line discipline) that you would need to modify. It feels a bit silly though to be sending something in a reply to the terminal sending "Please stop sending".

Another approach could be to wrap your shell session into some pseudo-terminal wrapper that puts the host terminal device in raw mode and offers its own pseudo-terminal device to bash, intercepts those ^S character to write the message.

That could be done with GNU screen for instance which is a terminal emulator within a terminal.

Add to your ~/.screenrc:

bindkey "\023" eval 'hstatus "Stopped - press Ctrl+Q to proceed"' xoff
bindkey "\021" eval "hstatus screen" xon

Here with the message issued in the hardstatus line of your terminal.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.