I need to read lines from STDIN and process them. I could do something like:

<print lines to STDOUT> | while read line; do
   <process $line>

But this means that first all lines are printed and piped and then they are processed. I want to process every single line immediately, eventually before the next lines are sent to STDOUT.

This might be useful if the lines should be processed while they are are generated (e.g. a log file). My task is to use zbar (warning! the web page crashes Firefox...) to scan several QR codes and to open its URLs in Firefox. If run zbarcam --raw the webcam is used to scan QR codes. If a QR code is found the URL is printed to STDOUT immediately, but zbarcam does not terminate. So I'd like to read this line and to run firefox $url or so (but without waiting for other URLs).

I've found a solution, but I'm wondering if this can't be done easier. Basically he writes the output of zbarcam to a temporary file and waits until something is written there:

zbarcam --raw > tmpfile &
while [[ ! -s $tmp ]] ; do
  sleep 1

Can this be done with out temporary files and without a loop with sleep 1?

3 Answers 3


Your first attempt does work:

tail -f temp.file | while read LINE; do firefox $LINE; done

Then in another terminal, append to the temp file so that tail -f prints a line to STDOUT:

echo 'google.com' >> temp.file

Every time you do this, firefox will open a new tab.

  • Well, that's really strange. I was fighting like 2 or 3 hours with this problem. And now it simply works as expected... :) One hint for others: A firefox process should be running already!
    – lumbric
    Commented Oct 11, 2012 at 12:18
  • Since you say zbar --raw does not terminate, this sounds like everything you wanted. Is that so? Anyway, it's best to also quote $LINE, so urls with ampersands don't break the loop. Commented Oct 13, 2012 at 19:12

After trying around a bit more, I've found a partial solution:

zbarcam --raw|xargs -L1 firefox

The crucial part is the parameter -L1, which tells xargs to read only one single line. After reading the first QR code it runs firefox. zbar then still runs, but I can't scan a second QR code (xargs has to be called a second time somehow).


very, very close:

zbarcam --raw | xargs -n 1 chromium-browser

This works beautifully for me, every scan ends up in a new tab.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .