Piping to a function

Published: Wednesday, Dec 1, 2010 Last modified: Saturday, Mar 23, 2024

#!/bin/sh
t() {
	if ! test -t 0 # If stdin is open
	then
		cat # echo it
	else
		echo hello # otherwise print hello
	fi
}

echo foo bar | t
t
t

Results in:

hendry@webconverger test$ sh bar.sh 
foo bar
hello
hello