3.3 Redirecting: “<”

Redirecting STDIN

Many standard commands of BASH like “head” are reading input from STDIN if no file is provided.

Using “head” on a file

ls > ls.txt
cat ls.txt
#ls.all
#ls.err
#ls.txt
#out.txt
head -n2 ls.txt
#ls.all
#ls.err

We can use head from STDIN

head -n2 #press Enter
#line 1
#line 1
#line 2
#line 2

In the example above, “head -n2” will print 2 lines in the screen but Enter is pressed without providing a file name, so we will have to type two new lines in order to validate the condition.

STDIN can be also redirected to a file using the operator “<”. However, in many cases this is redundant.

head -n2 < ls.txt
#ls.all
#ls.err