Why isn't there a newline when I execute a command?

The following code just runs a command:

use std::process::Command;

fn main() {
    let mut command = Command::new("script");

    command.arg("-qec").arg("sleep 4").arg("/dev/null");

    let mut child = command.spawn().expect("failed to spawn command");

    child.wait().expect("editor exited with a non-zero exit");
}

When I press Ctrl + C to interrupt the command, I see this:

^Calex@alex-M52AD-M12AD-A-F-K31AD:~/rust/ctrl-raw$

I was expecting to see a newline after ^C:

^C
alex@alex-M52AD-M12AD-A-F-K31AD:~/rust/ctrl-raw$

Why isn't there a newline?

Note: If I execute sleep 4 directly in the terminal, I see a newline:

alex@alex-M52AD-M12AD-A-F-K31AD:~/rust/ctrl-raw$ sleep 4                                                                                                                       
^C                                                                                                                                                                      
alex@alex-M52AD-M12AD-A-F-K31AD:~/rust/ctrl-raw$

Original post.

1 Like

That's not the command you're running in your code though, what happens if you interrupt script -qec "sleep 4" /dev/null in the terminal? I don't get a newline in that case.

4 Likes

Hey, you're right. I wonder why that is. I'll do some research. Thanks for spotting that!

How can I add a newline after ^C, though, while still using script?

It calls ioctl. Not spend time figuring out which setting. See description of a few(/all maybe, I don't know) in stty. Can see the calls by running with strace.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.