Here document as an argument to bash function Here document as an argument to bash function bash bash

Here document as an argument to bash function


The way to that would be possible is:

printArgs 17 "$(cat <<EOF1819EOF)"

But why would you want to use a heredoc for this? heredoc is treated as a file in the arguments so you have to (ab)use cat to get the contents of the file, why not just do something like:

print Args 17 "1819"

Please keep in mind that it is better to make a script on the machine you want to ssh to and run that then trying some hack like this because bash will still expand variables and such in your multiline argument.


If you're not using something that will absorb standard input, then you will have to supply something that does it:

$ foo () { while read -r line; do var+=$line; done; }$ foo <<EOFabcEOF


Building on Ned's answer, my solution allows the function to take its input as an argument list or as a heredoc.

printArgs() (  [[ $# -gt 0 ]] && exec <<< $*  ssh -T remotehost)

So you can do this

printArgs uname

or this

printArgs << EOFunameuptimeEOF

So you can use the first form for single commands and the long form for multiple commands.