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?

2 answers

  • answered 2018-01-14 10:08 sjsam

    $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.

  • answered 2018-01-14 14:35 chepner

    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.