kamal
kamal

Reputation: 1034

What does the redirection operator "<" do in shell scripts?

I came across the below shell command:

$prog.sh < file_name.json

I know it reads from a file, but how and where does prog.sh load the file?

Upvotes: 0

Views: 110

Answers (2)

sjsam
sjsam

Reputation: 21965

$prog.sh < file_name.json

As you rightly guess. The < is meant for redirecting the input from a file so that your script will read from the file which will be the (temporary) stdin(fd0).

it read from a file, but how and where prog.sh will load the file

It depends on how you plan to go about it. Any command in the script that expects an input from the stdin will now read from the file. The new line character in the text file (usually) stands for the in the stdin.

Upvotes: 1

chepner
chepner

Reputation: 531075

Every program has three open file handles at startup, one of which is standard input. Normally, the file handles are inherited from the parent process. The < operator tells the shell that, instead of passing its standard input to prog.sh, to open file_name.json instead and give that file handle to prog.sh as its standard input.

Upvotes: 2

Related Questions