Echo multi-line string via heredoc syntax

[user@freegtw ~]$ cat bashtest
#!/usr/local/bin/bash
echo <<<"EOF"
line1
line2
line3
EOF

[user@freegtw ~]$ ./bashtest

./bashtest: line 3: line1: command not found
./bashtest: line 4: line2: command not found
./bashtest: line 5: line3: command not found
./bashtest: line 6: EOF: command not found

What am i doing wrong?

Either

cat <<"EOF"
line1
line2
line3
EOF

Or, much more efficient

echo "\
line1
line2
line3"

The same

[user@freegtw ~]$ cat bashtest
#!/usr/local/bin/bash
cat <<<"EOF"
line1
line2
line3
EOF

[user@freegtw ~]$ ./bashtest
EOF
./bashtest: line 3: line1: command not found
./bashtest: line 4: line2: command not found
./bashtest: line 5: line3: command not found
./bashtest: line 6: EOF: command not found

OS: FreeBSD 9.1

<<< has a rather different meaning to bash -- it redirects a single line.

You should be doing <<, not <<<.

Nice one, Corona! Thx.

1 Like