linux cat into file including exiting code all from copy+paste

1

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?

719016

Posted 2014-10-17T08:24:13.460

Reputation: 2 899

Answers

5

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

Tuomas

Posted 2014-10-17T08:24:13.460

Reputation: 231

3

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.

Raúl Salinas-Monteagudo

Posted 2014-10-17T08:24:13.460

Reputation: 1 058