How to read from a file or standard input in Bash

The following Perl script (my.pl) can read from either the file in the command line arguments or from standard input (STDIN):

while (<>) {
   print($_);
}

perl my.pl will read from standard input, while perl my.pl a.txt will read from a.txt. This is very handy.

Is there an equivalent in Bash?

20 Answers
20

Leave a Comment