Why does the console need sometimes a reset after CTRL+C

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
9
down vote

favorite
3












Some command line interface tools return a broken console when canceled by CTRL+C.
Sometimes the text is invisible, or there are graphic problems until I run the command reset.



(I use bash, but expect it is independent of the shell.)



Does this effect have a name?
What causes this, and how can programmers prevent this in the tools?
Is there a strategy how this problem is addressed in the major programming languages?







share|improve this question




















  • Related questions are unix.stackexchange.com/questions/401010 , unix.stackexchange.com/questions/79684 , unix.stackexchange.com/questions/164944 , unix.stackexchange.com/questions/282514 , and unix.stackexchange.com/questions/398291 just for starters. See also superuser.com/questions/1259442 .
    – JdeBP
    Nov 11 '17 at 16:53











  • stty sane for the grey beards
    – Thorbjørn Ravn Andersen
    Nov 11 '17 at 23:23














up vote
9
down vote

favorite
3












Some command line interface tools return a broken console when canceled by CTRL+C.
Sometimes the text is invisible, or there are graphic problems until I run the command reset.



(I use bash, but expect it is independent of the shell.)



Does this effect have a name?
What causes this, and how can programmers prevent this in the tools?
Is there a strategy how this problem is addressed in the major programming languages?







share|improve this question




















  • Related questions are unix.stackexchange.com/questions/401010 , unix.stackexchange.com/questions/79684 , unix.stackexchange.com/questions/164944 , unix.stackexchange.com/questions/282514 , and unix.stackexchange.com/questions/398291 just for starters. See also superuser.com/questions/1259442 .
    – JdeBP
    Nov 11 '17 at 16:53











  • stty sane for the grey beards
    – Thorbjørn Ravn Andersen
    Nov 11 '17 at 23:23












up vote
9
down vote

favorite
3









up vote
9
down vote

favorite
3






3





Some command line interface tools return a broken console when canceled by CTRL+C.
Sometimes the text is invisible, or there are graphic problems until I run the command reset.



(I use bash, but expect it is independent of the shell.)



Does this effect have a name?
What causes this, and how can programmers prevent this in the tools?
Is there a strategy how this problem is addressed in the major programming languages?







share|improve this question












Some command line interface tools return a broken console when canceled by CTRL+C.
Sometimes the text is invisible, or there are graphic problems until I run the command reset.



(I use bash, but expect it is independent of the shell.)



Does this effect have a name?
What causes this, and how can programmers prevent this in the tools?
Is there a strategy how this problem is addressed in the major programming languages?









share|improve this question











share|improve this question




share|improve this question










asked Nov 11 '17 at 14:26









Jonas Stein

1,02721033




1,02721033











  • Related questions are unix.stackexchange.com/questions/401010 , unix.stackexchange.com/questions/79684 , unix.stackexchange.com/questions/164944 , unix.stackexchange.com/questions/282514 , and unix.stackexchange.com/questions/398291 just for starters. See also superuser.com/questions/1259442 .
    – JdeBP
    Nov 11 '17 at 16:53











  • stty sane for the grey beards
    – Thorbjørn Ravn Andersen
    Nov 11 '17 at 23:23
















  • Related questions are unix.stackexchange.com/questions/401010 , unix.stackexchange.com/questions/79684 , unix.stackexchange.com/questions/164944 , unix.stackexchange.com/questions/282514 , and unix.stackexchange.com/questions/398291 just for starters. See also superuser.com/questions/1259442 .
    – JdeBP
    Nov 11 '17 at 16:53











  • stty sane for the grey beards
    – Thorbjørn Ravn Andersen
    Nov 11 '17 at 23:23















Related questions are unix.stackexchange.com/questions/401010 , unix.stackexchange.com/questions/79684 , unix.stackexchange.com/questions/164944 , unix.stackexchange.com/questions/282514 , and unix.stackexchange.com/questions/398291 just for starters. See also superuser.com/questions/1259442 .
– JdeBP
Nov 11 '17 at 16:53





Related questions are unix.stackexchange.com/questions/401010 , unix.stackexchange.com/questions/79684 , unix.stackexchange.com/questions/164944 , unix.stackexchange.com/questions/282514 , and unix.stackexchange.com/questions/398291 just for starters. See also superuser.com/questions/1259442 .
– JdeBP
Nov 11 '17 at 16:53













stty sane for the grey beards
– Thorbjørn Ravn Andersen
Nov 11 '17 at 23:23




stty sane for the grey beards
– Thorbjørn Ravn Andersen
Nov 11 '17 at 23:23










2 Answers
2






active

oldest

votes

















up vote
14
down vote













A console sometimes needs a reset(1) (or some stty(1) command) because the state of a pseudo-terminal does not change when some process (e.g. a program started by your shell) terminates.



Read the tty demystified.



(I find the handling of pseudo-terminals and pseudottys the most difficult part of Linux)




Is there a strategy how this problem is addressed in the major programming languages?




A well-behaved program dealing with the terminal and changing its mode or line discipline should try hard to avoid crashing and issue the appropriate calls (see termios(3)) to put the terminal in the right state. BTW, libraries like ncurses or readline are helpful (but you need to call their cleanup routines appropriately).



See signal(7) and signal-safety(7). Avoiding crashing in your code is difficult. Read about undefined behavior.



An imperfect workaround could be to define a shell function which runs your program then does a reset (that could sometimes be inappropriate).






share|improve this answer






















  • That workaround isn't great; sometimes reset can result in different stty settings from the original ones.
    – Bob
    Nov 11 '17 at 15:25











  • Yes, thanks for pointing that out. I added "imperfect".
    – Basile Starynkevitch
    Nov 11 '17 at 15:35










  • I have read your links, they were interesting, but it would be helpfull, if you could add a pointer to a section for this Q/A each. After reading tty demystified, I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same.
    – Jonas Stein
    Nov 12 '17 at 1:04

















up vote
2
down vote













Responding to this problem isn't completely shell-independent. In zsh, there is the ttyctl builtin, which can "freeze" or "unfreeze" the tty mode. I don't think there's an equivalent in bash. The setty command in tcsh does the same thing, but more fine-grained: you can freeze individual settings.



Freezing the tty mode just means that zsh will remember the current mode, and if some future child changes it, the mode will be restored when the child suspends or terminates.



This will protect you from some of the bad effects of programs that crash or otherwise fail to clean up the terminal. You must remember to unfreeze if you want to make a change with stty, otherwise the shell will immediately undo whatever your stty did.



reset does more than restore stty modes, so you still might need it sometimes, but not often.






share|improve this answer




















  • Indeed: I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same. I tried the same with tcsh, but I could not crash my command by CTRL+C. The colors remained fine.
    – Jonas Stein
    Nov 12 '17 at 1:10










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403906%2fwhy-does-the-console-need-sometimes-a-reset-after-ctrlc%23new-answer', 'question_page');

);

Post as a guest






























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
14
down vote













A console sometimes needs a reset(1) (or some stty(1) command) because the state of a pseudo-terminal does not change when some process (e.g. a program started by your shell) terminates.



Read the tty demystified.



(I find the handling of pseudo-terminals and pseudottys the most difficult part of Linux)




Is there a strategy how this problem is addressed in the major programming languages?




A well-behaved program dealing with the terminal and changing its mode or line discipline should try hard to avoid crashing and issue the appropriate calls (see termios(3)) to put the terminal in the right state. BTW, libraries like ncurses or readline are helpful (but you need to call their cleanup routines appropriately).



See signal(7) and signal-safety(7). Avoiding crashing in your code is difficult. Read about undefined behavior.



An imperfect workaround could be to define a shell function which runs your program then does a reset (that could sometimes be inappropriate).






share|improve this answer






















  • That workaround isn't great; sometimes reset can result in different stty settings from the original ones.
    – Bob
    Nov 11 '17 at 15:25











  • Yes, thanks for pointing that out. I added "imperfect".
    – Basile Starynkevitch
    Nov 11 '17 at 15:35










  • I have read your links, they were interesting, but it would be helpfull, if you could add a pointer to a section for this Q/A each. After reading tty demystified, I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same.
    – Jonas Stein
    Nov 12 '17 at 1:04














up vote
14
down vote













A console sometimes needs a reset(1) (or some stty(1) command) because the state of a pseudo-terminal does not change when some process (e.g. a program started by your shell) terminates.



Read the tty demystified.



(I find the handling of pseudo-terminals and pseudottys the most difficult part of Linux)




Is there a strategy how this problem is addressed in the major programming languages?




A well-behaved program dealing with the terminal and changing its mode or line discipline should try hard to avoid crashing and issue the appropriate calls (see termios(3)) to put the terminal in the right state. BTW, libraries like ncurses or readline are helpful (but you need to call their cleanup routines appropriately).



See signal(7) and signal-safety(7). Avoiding crashing in your code is difficult. Read about undefined behavior.



An imperfect workaround could be to define a shell function which runs your program then does a reset (that could sometimes be inappropriate).






share|improve this answer






















  • That workaround isn't great; sometimes reset can result in different stty settings from the original ones.
    – Bob
    Nov 11 '17 at 15:25











  • Yes, thanks for pointing that out. I added "imperfect".
    – Basile Starynkevitch
    Nov 11 '17 at 15:35










  • I have read your links, they were interesting, but it would be helpfull, if you could add a pointer to a section for this Q/A each. After reading tty demystified, I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same.
    – Jonas Stein
    Nov 12 '17 at 1:04












up vote
14
down vote










up vote
14
down vote









A console sometimes needs a reset(1) (or some stty(1) command) because the state of a pseudo-terminal does not change when some process (e.g. a program started by your shell) terminates.



Read the tty demystified.



(I find the handling of pseudo-terminals and pseudottys the most difficult part of Linux)




Is there a strategy how this problem is addressed in the major programming languages?




A well-behaved program dealing with the terminal and changing its mode or line discipline should try hard to avoid crashing and issue the appropriate calls (see termios(3)) to put the terminal in the right state. BTW, libraries like ncurses or readline are helpful (but you need to call their cleanup routines appropriately).



See signal(7) and signal-safety(7). Avoiding crashing in your code is difficult. Read about undefined behavior.



An imperfect workaround could be to define a shell function which runs your program then does a reset (that could sometimes be inappropriate).






share|improve this answer














A console sometimes needs a reset(1) (or some stty(1) command) because the state of a pseudo-terminal does not change when some process (e.g. a program started by your shell) terminates.



Read the tty demystified.



(I find the handling of pseudo-terminals and pseudottys the most difficult part of Linux)




Is there a strategy how this problem is addressed in the major programming languages?




A well-behaved program dealing with the terminal and changing its mode or line discipline should try hard to avoid crashing and issue the appropriate calls (see termios(3)) to put the terminal in the right state. BTW, libraries like ncurses or readline are helpful (but you need to call their cleanup routines appropriately).



See signal(7) and signal-safety(7). Avoiding crashing in your code is difficult. Read about undefined behavior.



An imperfect workaround could be to define a shell function which runs your program then does a reset (that could sometimes be inappropriate).







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 11 '17 at 17:47

























answered Nov 11 '17 at 14:31









Basile Starynkevitch

7,9081940




7,9081940











  • That workaround isn't great; sometimes reset can result in different stty settings from the original ones.
    – Bob
    Nov 11 '17 at 15:25











  • Yes, thanks for pointing that out. I added "imperfect".
    – Basile Starynkevitch
    Nov 11 '17 at 15:35










  • I have read your links, they were interesting, but it would be helpfull, if you could add a pointer to a section for this Q/A each. After reading tty demystified, I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same.
    – Jonas Stein
    Nov 12 '17 at 1:04
















  • That workaround isn't great; sometimes reset can result in different stty settings from the original ones.
    – Bob
    Nov 11 '17 at 15:25











  • Yes, thanks for pointing that out. I added "imperfect".
    – Basile Starynkevitch
    Nov 11 '17 at 15:35










  • I have read your links, they were interesting, but it would be helpfull, if you could add a pointer to a section for this Q/A each. After reading tty demystified, I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same.
    – Jonas Stein
    Nov 12 '17 at 1:04















That workaround isn't great; sometimes reset can result in different stty settings from the original ones.
– Bob
Nov 11 '17 at 15:25





That workaround isn't great; sometimes reset can result in different stty settings from the original ones.
– Bob
Nov 11 '17 at 15:25













Yes, thanks for pointing that out. I added "imperfect".
– Basile Starynkevitch
Nov 11 '17 at 15:35




Yes, thanks for pointing that out. I added "imperfect".
– Basile Starynkevitch
Nov 11 '17 at 15:35












I have read your links, they were interesting, but it would be helpfull, if you could add a pointer to a section for this Q/A each. After reading tty demystified, I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same.
– Jonas Stein
Nov 12 '17 at 1:04




I have read your links, they were interesting, but it would be helpfull, if you could add a pointer to a section for this Q/A each. After reading tty demystified, I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same.
– Jonas Stein
Nov 12 '17 at 1:04












up vote
2
down vote













Responding to this problem isn't completely shell-independent. In zsh, there is the ttyctl builtin, which can "freeze" or "unfreeze" the tty mode. I don't think there's an equivalent in bash. The setty command in tcsh does the same thing, but more fine-grained: you can freeze individual settings.



Freezing the tty mode just means that zsh will remember the current mode, and if some future child changes it, the mode will be restored when the child suspends or terminates.



This will protect you from some of the bad effects of programs that crash or otherwise fail to clean up the terminal. You must remember to unfreeze if you want to make a change with stty, otherwise the shell will immediately undo whatever your stty did.



reset does more than restore stty modes, so you still might need it sometimes, but not often.






share|improve this answer




















  • Indeed: I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same. I tried the same with tcsh, but I could not crash my command by CTRL+C. The colors remained fine.
    – Jonas Stein
    Nov 12 '17 at 1:10














up vote
2
down vote













Responding to this problem isn't completely shell-independent. In zsh, there is the ttyctl builtin, which can "freeze" or "unfreeze" the tty mode. I don't think there's an equivalent in bash. The setty command in tcsh does the same thing, but more fine-grained: you can freeze individual settings.



Freezing the tty mode just means that zsh will remember the current mode, and if some future child changes it, the mode will be restored when the child suspends or terminates.



This will protect you from some of the bad effects of programs that crash or otherwise fail to clean up the terminal. You must remember to unfreeze if you want to make a change with stty, otherwise the shell will immediately undo whatever your stty did.



reset does more than restore stty modes, so you still might need it sometimes, but not often.






share|improve this answer




















  • Indeed: I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same. I tried the same with tcsh, but I could not crash my command by CTRL+C. The colors remained fine.
    – Jonas Stein
    Nov 12 '17 at 1:10












up vote
2
down vote










up vote
2
down vote









Responding to this problem isn't completely shell-independent. In zsh, there is the ttyctl builtin, which can "freeze" or "unfreeze" the tty mode. I don't think there's an equivalent in bash. The setty command in tcsh does the same thing, but more fine-grained: you can freeze individual settings.



Freezing the tty mode just means that zsh will remember the current mode, and if some future child changes it, the mode will be restored when the child suspends or terminates.



This will protect you from some of the bad effects of programs that crash or otherwise fail to clean up the terminal. You must remember to unfreeze if you want to make a change with stty, otherwise the shell will immediately undo whatever your stty did.



reset does more than restore stty modes, so you still might need it sometimes, but not often.






share|improve this answer












Responding to this problem isn't completely shell-independent. In zsh, there is the ttyctl builtin, which can "freeze" or "unfreeze" the tty mode. I don't think there's an equivalent in bash. The setty command in tcsh does the same thing, but more fine-grained: you can freeze individual settings.



Freezing the tty mode just means that zsh will remember the current mode, and if some future child changes it, the mode will be restored when the child suspends or terminates.



This will protect you from some of the bad effects of programs that crash or otherwise fail to clean up the terminal. You must remember to unfreeze if you want to make a change with stty, otherwise the shell will immediately undo whatever your stty did.



reset does more than restore stty modes, so you still might need it sometimes, but not often.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '17 at 22:11









Wumpus Q. Wumbley

4,0101120




4,0101120











  • Indeed: I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same. I tried the same with tcsh, but I could not crash my command by CTRL+C. The colors remained fine.
    – Jonas Stein
    Nov 12 '17 at 1:10
















  • Indeed: I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same. I tried the same with tcsh, but I could not crash my command by CTRL+C. The colors remained fine.
    – Jonas Stein
    Nov 12 '17 at 1:10















Indeed: I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same. I tried the same with tcsh, but I could not crash my command by CTRL+C. The colors remained fine.
– Jonas Stein
Nov 12 '17 at 1:10




Indeed: I started stty -a > /tmp/test1 in bash then a command, which I cancelled. The terminal color was now red. stty -a > /tmp/test2 but test1 and test2 were exactly the same. I tried the same with tcsh, but I could not crash my command by CTRL+C. The colors remained fine.
– Jonas Stein
Nov 12 '17 at 1:10

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f403906%2fwhy-does-the-console-need-sometimes-a-reset-after-ctrlc%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Displaying single band from multi-band raster using QGIS

How many registers does an x86_64 CPU actually have?