Search code examples
bashvariablespipeline

How to make variable infront of pipeline in bash?


I would like to save output of first command as variable in front of pipeline and send them to the pipe, too.

For example: find -type d | grep -E '^\./y'. And in my variable going to be output of find -type d.

Thanks for help

EDIT Maybe I can solve this problem another way, but I am standing in front of another problem. How to call my own function with parameter from pipeline?

EX: find -type d | MyFunction


Solution

  • RE:

    EDIT Maybe I can solve this problem another way, but I am standing in front of another problem. How to call my own function with parameter from pipeline?
    
    EX: find -type d | MyFunction
    

    The following all work:

    $ cat ./blah.sh
    #!/bin/bash
    function blah {
            while read i; do
                    echo $i
            done
    }    
    find ~/opt -type d | blah
    blah <<< $(find ~/opt -type d)
    blah < <(find ~/opt -type d)
    
    $ ./blah.sh
    /home/me/opt
    /home/me/opt/bin
    /home/me/opt /home/me/opt/bin
    /home/me/opt
    /home/me/opt/bin
    

    So I'd imagine if find -type d | MyFunction doesn't work, then the function is probably not looking for input on stdin.