alkar alkar - 10 months ago 71
Bash Question

Pass input to a script: stdin vs file path in argv

I write many simple scripts for testing or processing CSV files. Most of scripts have same logic: read one file and write the output to a console or another file. For such case I usually use the simplest for implementation approach: read from

, write to
, so invocation is like:

script < input.csv > output.csv

Can you give some points why I shall prefer this style:

script input.csv > output.csv

Or even:

script input.csv output.csv

Answer Source

This is how I deal with the same task now, similar to what @gniourf_gniourf's answer does (except my scripts are in Haskell).

When the program is invoked with arguments, like CMD INPUT1 [INPUT2 [...]], treat them as input files and ignore standard input, except for special case when one or more args are -.

When no args are given, read standard input. So CMD call without args is equivalent to CMD -.