Echo Control C character

In Bash, a ^C can be passed as an argument on the command-line (and thus passed to echo for echoing) by writing $'\cc'. Thus, the desired command is:

echo $'\cc' | ./program_that_does_not_terminate

No, piping a CTRL-C character into your process won't work, because a CTRL-C keystroke is captured by the terminal and translated into a kill signal sent to the process.

The correct character code (in ASCII) for CTRL-C is code number 3 but, if you echo that to your program, it will simply receive the character from its standard input. It won't cause the process to terminate.

If you wanted to ease the task of finding and killing the process, you could use something like:

./program_that_does_not_terminate &
pid=$!
sleep 20
kill ${pid}

$! will give you the process ID for the last started background process.

Tags:

Bash