Method 1: You can use the ‘read command’ with a “while loop” to read from a pipe within a bash script.
#!/bin/sh
cat names.txt | while read name; do
echo "$name"
done
Method 2: You can use the ‘read command’ to read from a pipe within a script in the terminal. For example,
echo “Hello World!” | { read name; echo “msg=$name”; }
Method 3: If you want to read a command through pipe, you need a script like this:
func.sh:
#!/bin/bash
function read_stdin()
{
cat > file.txt
}
read_stdin
This will take any command as input and redirect the output to file.txt. Try running:
date|./func.sh