I have written a shell script which processes a text input and displays the expected output, but post displaying the expected result - it waits for a ctrl+c in order for it to complete and for cursor to move to $_ space. How can I include this ctrl+c in my shell script itself ? or what is the shell script that does ctrl+c using bash/shell commands ?

The script is as follows:

sed 's/\xC2\xA0//g' | awk '$1=="|"{if(f){print f" "$2;f=""}else{f=$2}}' filename.txt

filename.txt contains: https://justpaste.it/edit/30310895/df1838c844c0fcd1

  • 6
    Your script should just exit after the job is done. There must be something wrong with your script, you should post it here. – SparedWhisle Sep 4 '19 at 4:45
  • What does the script do after it has processed the input, and how does it process the input? Please post the script in your question. – Kusalananda Sep 4 '19 at 6:59
  • @DavidDai updated the question – dynamicJos Sep 5 '19 at 3:29
  • 2
    @dynamicJos what do you need sed for? It doesn't do anything at all here. Just remove sed 's/\xC2\xA0//g' | from your script. – SparedWhisle Sep 5 '19 at 3:33

The pipeline that you show is essentially

sed expression | awk expression filename

This would run awk on the file filename while sed would sit around doing nothing while waiting for input from the user on its standard input (it would read what you typed on the terminal at that point).

To exit this script, you simply press Ctrl+D to signal the end of input to the sed process. When sed notices that there will be nothing more to read, it terminates and the script exits (since that was what was "pausing" it).

Your current script is more or less equivalent of running

awk expression filename
sed expression

... since the two commands are not communicating with each other over the pipe.

What you want to be doing is

sed expression filename | awk expression

Here, sed is operating on the file and sends the result over to awk for further processing. awk is started without a filename, which means it reads from its standard input, which is connected to the standard output of sed via the pipe.

In your case, you want

sed 's/\xC2\xA0//g' filename.txt | awk '$1=="|"{if(f){print f" "$2;f=""}else{f=$2}}'

(If that pipeline does what you want it to do or not, I don't know, but now at least the sed and awk processes would be piped correctly).


ctrl-c sends the "SIGINT" signal, so to send the same effect as ^C use

kill -SIGINT pid-of-the-script

  • how would I know pid of the shell that I am about to run ? – dynamicJos Sep 5 '19 at 3:31
  • 1
    if you launch it using exec it will have the same PID as the process that used exec, else you have to wait until after it is launced to find the pid – Jasen Sep 5 '19 at 4:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.