- use `read` to pipe in user input
- without `stdin` it waits for user input
- alternatively, if you pipe in data via `stdin`, it automatically displays the first line therein instead of waiting for user input
- `./simple.sh < test.text` outputs first line of `test.txt`
- example of piping data from `stin`into a script
- `-t` tests for a file descriptor
- if you simply run the script, it will return 0
- `./detect.sh`
- if you pipe in a file when running the script, it will return 1
- `./detect.sh < test.txt`
- `cat test.text | ./detect.sh`
```bash
#!/bin/bash
if [[ -t 0 ]]; then
echo "No stdin provided"
else
echo "I detect stdin!"
fi
echo `date`
```
![[images/Pasted image 20250808182320.png]]
- another example that reads a multi-line file via `stdin` but only allows a single line of interactive entry
- if no `stdin`, the first segment of the conditional completes and asks for user input
- if `stdin` exists, the second segment completes
- `read -d ''` corresponds to a null delimiter to output till end of file
```bash
#!/bin/bash
# first segment reads a single line of interactive input
# second segment reads in a multi-line file provided via stdin
echo "tell me something interesting..."
echo .
if [[ -t 0 ]]; then
read input
echo .
sleep 1
echo .
sleep 1
else
sleep 1
echo .
sleep 1
echo .
read -d '' input
echo "got it!"
echo .
sleep 1
fi
echo "here's what you provided me..."
echo
sleep 1
echo "$input"
echo `date`
```
![[images/Pasted image 20250808192829.png]]