In bash (as you have tagged it) you can do
cat <(echo -e '## Model answer\n\n```sql') query.sql <(echo -e '```\n') >> README.md
The <()
uses process substitution; the command in parentheses is run in a subshell and its output is inserted into the larger command line. This form allows you to run without stringing lots of commands together.
If you really want avoid running commands -- for speed's sake -- you can get fancier and do this:
cat <(echo -e '## Model answer\n\n```sql') query.sql - <<<'```'
This uses here strings instead of one command. The -
means use standard input as an input to cat, and the <<<'```'
at the end means send this string to standard input.
Finally the coup de grace. You can even do something like this; in your specific case it's a bit unwieldy but it demonstrates the concept:
$ cat /dev/fd/{4,5,6} query.sql /dev/fd/7 4<<<'### Model answer' 5<<<'' 6<<<'```sql' 7<<<'```'
### Model answer
```sql
Contents of query.sql
```
This means concatenate file descriptors 4, 5, and 6, query.sql, and file descriptor 7, while opening these file descriptors and sending the respective strings to them. (This doesn't require Linux [where /dev/fd
was developed] to work, bash interprets /dev/fd
itself and emulates it on systems that don't have it. This is why you can't do /dev/fd/[4-6]
.) I chose these file descriptors because 0-2 are reserved for stdin, stdout, and stderr, and 10 and above are reserved for bash, and I can't count correctly.