How to pipe HTML content to Safari via the command line

command linehtmlsafariterminal

I have a situation in which I'm getting HTML content via a pipe. I'd like to display it in a browser window. But the obvious step,

somethingThatProducesHTMLonStdout | open -f -a "Safari"

does not work: Safari displays the content, but as plain text; it does not render the HTML. I know for a fact that my HTML content is valid HTML content. In fact, it is easy to demonstrate this problem with a trivial test case:

echo "<html><body>foo</body></html>" | open -f -a "Safari"

does the same thing – displays plain text, not rendered content. I'm at a loss as to why Safari insists on doing this. What is the secret to making it render the HTML?

Note: I know I can make this work by saving the content to a temporary file and then opening the temporary file. I want to make it work without using a temporary file, if it is possible.

Best Answer

You can use something like

echo '<html><body><h2>Hi, there!</h2>foo</body></html>' > /tmp/x.html && open -a "Safari" /tmp/x.html

I entered some HTML that contains an exclamation mark to show that you need to use single quotes for the shell, otherwise it will interpret this as a history recall. Yes, longer and you need a file name, but you can use your somethingThatProducesHTMLonStdout tool from your question.