Search code examples
bashzshcatio-redirectionheredoc

Combining heredoc and input redirection in Bash vs ZSH


The following code works on ZSH with no issue, combining the heredoc with the content of the file test.csv:

cat <<EOF <test.csv
id,name,age
EOF

How can I write the same command in Bash?


Solution

  • $(<file) will work in both Bash and Zsh:

    cat <<EOF
    id,name,age
    $(<test.csv)
    EOF
    

    It will also work in Ksh (from where I believe it comes and was ported to Bash and Zsh). It behaves as $(cat file) except it will not call out to cat and will be handled completely by the shell itself.

    It is described in Bash documentation Command Substitution section:

    The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).