Why does `ack` not produce output when used with `bash` like this?

ack gets confused because stdin is a pipe rather than a terminal. You need to pass the --nofilter option to force ack to treat stdin as a tty.

This:

# ack.sh
ack --nofilter foobar file.txt

works:

$ cat ack.sh | bash
foobar

If you ask me, that behaviour is quite unexpected. Probably it is expected when someone understand the concepts of ack which I do not atm. I would expect that ack doesn't look at stdin when filename arguments are passed to it.


Why does unbuffer "solve" the problem?

unbuffer, following it's man page, does not attempt to read from stdin:

  Normally, unbuffer does not read from stdin.  This  simplifies  use  of
   unbuffer in some situations.  To use unbuffer in a pipeline, use the -p
   flag. ...

Looks like ack tries to be too! smart about stdin here. If it is empty it does not read from stdin and looks at the filenames passed to it. Again, imo it would be correct to not look at stdin at all if filename arguments are present.

Tags:

Bash

Ack