Firefox Configuration – How to Make Firefox Read stdin

firefox

echo '<h1>hello, world</h1>' |  firefox
cat index.html | firefox

These commands don't work.
If firefox can read stdin, I can send HTML to firefox via a pipe.
Is it possible to make firefox read stdin?

Best Answer

The short answer is, you're better off writing a temporary file and opening that. Getting pipes to work properly is more complicated and probably won't give you any extra advantages. That said, here's what I've found.

If your firefox command is actually starting Firefox instead of talking with an already-running Firefox instance, you can do this:

echo '<h1>hello, world</h1>' | firefox /dev/fd/0

Which tells Firefox explicitly to read its standard input, which is where the pipe is putting its data. But if Firefox is already running, the firefox command is just going to pass that name to the main Firefox process, which will read its own standard input, which probably won't give it anything and certainly isn't connected to your pipe.

Furthermore, when reading from a pipe, Firefox buffers things pretty heavily, so it's not going to update the page each time you give it a new line of HTML, if that's what you're going for. Try closing Firefox and running:

cat | firefox /dev/fd/0

(N.B. you do actually need the cat here.) Paste some long lines into your shell window repeatedly until Firefox decides to update the page, and you can see how much data it takes. Now send an End-Of-File signal by hitting Ctrl+D on a new line, and watch Firefox update instantly. But then you can't add any more data.

So best is probably:

echo '<h1>hello, world</h1>' >my_temporary_file; firefox my_temporary_file