Bash pipe
Posted on March 12, 2008
I did not know that I can use pipes with bash functions:
bash$ demo() { while read input; do echo “Hello” $input; done; }
bash$ ls | demo
Hello bin/
Hello boot/
Hello dead.letter
Hello dev/
Hello etc/
Hello home/
Hello initrd/
Hello lib/
Hello lost+found/
Hello media/
Hello mnt/
Hello opt/
Hello proc/
Hello root/
Hello sbin/
Hello sys/
Hello tmp/
Hello usr/
Hello var/
It’s even possible to have anonymous functions
ls -la | { read total; echo “And my total is $total”; while read line; do echo “Hello” $line; done; }
Filed Under Uncategorized | 2 Comments
2 Comments so far
Leave a Comment
If you would like to make a comment, please fill out the form below.
Wth is BASH?? How is it different/better from writing this same function in…say, C…
Bash is a command prompt http://en.wikipedia.org/wiki/Bash just like cmd.exe on windows.
You can write scripting programs and they are interpreted every time you run them.
C is a lower level programming language, and it is much more powerful, but bash and scripting languages in general are easer to use.