On a Unix-like terminal, if you want to use Control-C as the interrupt key instead of Delete, which stty command correctly sets the interrupt character?

Difficulty: Easy

Correct Answer: stty intr \^c

Explanation:


Introduction / Context:
The terminal driver maps certain key combinations to control signals such as interrupt (INTR), quit, and erase. Customizing these mappings with 'stty' is useful on atypical keyboards or when your environment defaults differ. Here, we want Control-C to serve as the interrupt key that sends SIGINT to foreground processes.


Given Data / Assumptions:

  • You have a POSIX shell and the 'stty' utility available.
  • Goal: set the interrupt character to Control-C.
  • Shell may interpret carets, so escaping may be necessary.


Concept / Approach:
Use 'stty intr ^c' to set the INTR character. In many shells, the caret sequence is interpreted by 'stty' itself, but to avoid shell expansion issues, it is common to escape the caret as '\^c'. This ensures the literal control notation is passed correctly. After this change, pressing Control-C sends SIGINT to the foreground job as expected.


Step-by-Step Solution:

Check current settings: stty -a | grep intrSet interrupt to Ctrl-C: stty intr \^cTest: run a long command and press Ctrl-C to interrupt.Persist setting by adding the command to your shell startup file if desired.


Verification / Alternative check:
Run 'stty -a' again to see 'intr = ^C' reported. Start 'sleep 100' and press Ctrl-C; it should terminate with SIGINT.


Why Other Options Are Wrong:

  • tty ^c: 'tty' prints terminal name; it does not set control chars.
  • stty echoe / stty echo \^a: These adjust echo behavior or unrelated settings; they do not set INTR to Ctrl-C.
  • None of the above: Incorrect because 'stty intr \^c' is correct and safe.


Common Pitfalls:
Forgetting to escape special characters in some shells, or confusing 'erase' with 'intr'. Also note that some environments already default to ^C for interrupt.


Final Answer:
stty intr \^c

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion