2

Can I print out a code to STDOUT that would mimic the behavior of Ctrl+c inside cat? E.g. I would like my script to print out the cat command followed by the content of the file like this:

cat > /my/file/name.txt
I want this line in the filename above
and this one
and this one as well but I want to exit cat in the next line
Ctrl+c somehow

The behavior I want is for the user to copy the lines above, then paste them in a terminal window, and without any more typing, have a name.txt file with the three lines of content, saved, and be back to the interactive terminal prompt.

Any ideas?

2 Answers 2

6

Use the here document feature:

cat > /my/file/name.txt <<EOF
I want this line in the filename above
and this one
and this one as well but I want to exit cat in the next line
EOF
5

You do not even need a here document if you press Ctrl+D, which signals the end of the input.

Ctrl+C means to SIGINT the process (to send the interrupt signal), whereas Ctrl+D gives and end-of-file to the process currently reading from the standard input.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .