user1569030
user1569030

Reputation:

cat multiple files and subshell

Is there a single shell command that will do the following:

cp header page.html
markdown page.md >> page.html
cat footer >> page.html

(Markdown outputs to stdout by default.)

A colleague suggested

echo $(cat header) $(markdown page.md) $(cat footer) >> page.html

But apart from three subshells and two cats, which will probably win me a "useless use of cat" award, it also strips newlines - which is no good, especially in the <pre><code> blocks.

What I'd like is something like this (which obviously doesn't work)

cat header $(markdown page.md) footer > page.html

where I can tell cat to use the output of the subshell for one of the files to read from. Ideally without setting up any temporary files, named pipes etc.

Upvotes: 2

Views: 1045

Answers (2)

Aaron
Aaron

Reputation: 24812

Since you only have a single subshell I'd use this :

markdown page.md | cat header - footer > page.html

The - in the cat params refers to stdin, which is populated by the markdown command.

If you had multiple subshells, I'd recommend using the solution anishane commented about, process substitution :

cat header <(markdown page1.md) <(markdwon page2.md) footer > page.html

Upvotes: 6

Tiago Lopo
Tiago Lopo

Reputation: 7959

That should do:

(cat header;  markdown page.md;  cat footer) >   page.html

Upvotes: 2

Related Questions