Great video on stdin vs stdout: https://www.youtube.com/watch?v=OsErpB0-mWY
As a standard your input usually comes from the keyboard. Ex cat file.txt
. <- here you're passing file.txt
as a parameter.
However you can do cat < $file_name
i.e. make the input to the cat
command a variable.
Both cat file.txt
& cat < file.txt
achieve the same, but through different mechanics. <
is passing something as an input.
I suppose it's because the cat
command has abilities to handle file names as arguments and through stdin
Similarly the standard output of your cat
command is the console.
However you can redirect that output to another file using >
. Ex echo "hello people" > ppl.txt
.
The difference between >>
and >
is that:
>>
will append to the file.>
will replace everything in the file.
Use /dev/stdin/
Example:
reader.sh
#!/bin/bash
while read line
do
echo "$line"
done < "${1:-/dev/stdin}"
Usage:
sh reader.sh file.txt
sh reader.sh < file.txt
It's a form of defaulting / fallback recovery. From here
echo ${parameter:-33}
# echos 33; because parameter is unset/undefined
parameter=55
echo ${parameter:-33}
# echos 55