Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell expansion with heredoc

Tags:

bash

How does one for subshell expansion in heredoc for all command?

eg:

file=report_$(date +%Y%m%d)
cat <<EOF > $file
    date
    hostname
    echo 'End of Report'
EOF

such that all the commands are evaluated?

I am aware

file=report_$(date +%Y%m%d)
cat <<EOF > $file
    $(date))
    $(hostname)
    $(echo 'End of Report')
EOF

would work, but is there a way to specify subshell by default?

like image 659
Lighthart Avatar asked Apr 29 '26 00:04

Lighthart


1 Answers

You could use sh (or bash) as the command instead of cat; that will actually run it as shell script:

sh <<EOF > $file
    date
    hostname
    echo 'End of Report'
EOF
like image 109
Explosion Pills Avatar answered Apr 30 '26 15:04

Explosion Pills



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!