Home > other >  bash script stdin not detected clarification
bash script stdin not detected clarification

Time:07-26

This kind of got me confused. this is my bash script:

filename: reader.sh

READ = $("cat")
echo "$READ"

So the first line reads stdin and the second line prints it. Nevertheless, I get that when I start my terminal and start pressing keys on my keyboard it will pop up in the terminal due to the fact that the shell redirects stdin and stdout to e.g dev/pts/0, meaning that the file is used as stdin and also stdout. Afterwards the shell (when return is found by the tty driver) kind of saves the first argument of the command line which is the utility, and looks at the rest of the command linux, then it puts a sort of array or list of arguments in the environment of the program that is being called so it can use arguments. Why is it that the above bash script can print the output of a file through a piped stdin e.g ./reader.sh < otherfile, but not just ./reader.sh? I would expect in the second example that what's in stdin would be read from what was in dev/pts/0 since that's also just stdin.

Is it because when the arguments are parsed into a list, the dev/pts/0 file gets emptied?

CodePudding user response:

When you use

./reader.sh < otherfile

stdin in the script is connected to the file, not /dev/pts/0. cat inherits this stdin, so it reads from the file.

  • Related