LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Ctrl-C to echo ^C or not (https://www.linuxquestions.org/questions/linux-software-2/ctrl-c-to-echo-%5Ec-or-not-810219/)

Skaperen 05-26-2010 08:53 AM

Ctrl-C to echo ^C or not
 
I've noticed that in some distributions, or even some versions of distributions, pressing Ctrl-C at the bash command prompt either results in ^C being echoed, or not. I've been looking in "man bash" for a setting that would affect that (it doesn't apply when outside of bash, so I presume it is just something bash does or has set) but cannot find it. Anyone know what to do in bash to change this? If it matters, I want to turn it off where it is on.

evo2 05-27-2010 02:44 AM

I think this behavior can be controlled with the stty command. Perhaps there is something in the bash startup scripts that is toggling an option.

Cheers,

Evo2.

catkin 05-27-2010 03:41 AM

Quote:

Originally Posted by evo2 (Post 3982634)
I think this behavior can be controlled with the stty command.

I thought so too, netsearched for how and experimented a little but did not come up with an answer. I have noticed that when a script or a loop entered at the command prompt is running and Ctrl+C is used, then ^C is displayed.
Code:

c@CW8:~$ while true; do sleep 1; done
^C


Skaperen 06-01-2010 08:12 AM

I just tried running bash under strace to see if bash (or some library linked into its process) was really the culprit. See the 6th line:

Code:

read(0, 0x7fffddc44e9f, 1)              = ? ERESTARTSYS (To be restarted)
--- SIGINT (Interrupt) @ 0 (0) ---
rt_sigreturn(0x2)                      = -1 EINTR (Interrupted system call)
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
rt_sigprocmask(SIG_SETMASK, [], NULL, 8) = 0
write(2, "^C", 2)                      = 2
ioctl(2, TCXONC, TCOON)                = 0
rt_sigprocmask(SIG_BLOCK, [INT], [], 8) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig -icanon -echo ...}) = 0
ioctl(0, SNDCTL_TMR_STOP or TCSETSW, {B38400 opost isig icanon echo ...}) = 0
ioctl(0, SNDCTL_TMR_TIMEBASE or TCGETS, {B38400 opost isig icanon echo ...}) = 0


lavajoe 01-04-2011 01:52 PM

Quote:

Originally Posted by catkin (Post 3982668)
I thought so too, netsearched for how and experimented a little but did not come up with an answer. I have noticed that when a script or a loop entered at the command prompt is running and Ctrl+C is used, then ^C is displayed.
Code:

c@CW8:~$ while true; do sleep 1; done
^C


There are a couple of different situations here. If you hit the interrupt character while a program is running, the Linux kernel tty driver will echo "^C" (assuming you are using the default setting of "stty intr", which is ^C) if "stty echoctl" is set. Note that this did not work before kernel 2.6.25, so depending on the version in your Linux distro, you may or may not see this fix (but make sure you have "stty echoctl" set, or you will not see "^C" in any case).

If you hit ctrl-C on the bash command line, however, it is bash's readline code that will echo "^C". This is new bash behavior was introduced in bash 4.0. In bash 4.1, a setting was added to control this readline feature: "echo-control-characters". So putting "set echo-control-characters off" in your .inputrc file will suppress "^C" echo on the bash command line (if using bash 4.1 or later).

-Joe

catkin 01-04-2011 09:40 PM

Welcome to LQ lavajoe :)

Thank you for the very lucid and complete explanation.


All times are GMT -5. The time now is 04:48 AM.