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 so far
  1. 00se March 12, 2008 8:21 pm

    Wth is BASH?? How is it different/better from writing this same function in…say, C…

  2. elendal March 12, 2008 10:45 pm

    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.

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name

Email

Website

Comments

For spam detection purposes, please copy the number 5800 to the field below:

© Copyright 0xDEADBEEFCAFE • Powered by Wordpress • Design by Sebastin.

free web hit counter