bashhttp-redirectstdinio-redirection

Send string to stdin


Is there a way to effectively do this in bash:

/my/bash/script < echo 'This string will be sent to stdin.'

I'm aware that I could pipe the output from the echo such as this:

echo 'This string will be piped to stdin.' | /my/bash/script

Solution

  • You can use one-line heredoc

    cat <<< "This is coming from the stdin"
    

    the above is the same as

    cat <<EOF
    This is coming from the stdin
    EOF
    

    or you can redirect output from a command, like

    diff <(ls /bin) <(ls /usr/bin)
    

    or you can read as

    while read line
    do
       echo =$line=
    done < some_file
    

    or simply

    echo something | read param