Solved Piping a heredoc

Is it possible to pipe a heredoc?

I'm trying to convert a list of entries, each of which is on a separate line into a single line...

something like

Code:
cat <<EOF | tr '\n' '' >list
abc
xyz
EOF

where list would contain abc xyz
 

Actually it would have worked if I had written it correctly!!!

Code:
cat <<EOF | tr '\n' ' ' >list
abc
xyz
EOF

I missed out the space :)
 
[…] into a single line […]
As per POSIX™ definition a line is “[a] sequence of zero or more non‑<newline> characters plus a terminating <newline> character.” That means you want to transliterate all but the last newline character. Apart from that using cat(1) with a single argument (here the implicit use of standard input) especially without any options is really unnecessary. You can <<EOF to the first command in your pipeline. If you would like to split your pipeline’s commands into separate lines, you can use braces (that is a list). It also maintains a cleaner structure:​
Bash:
# Presuming there is no unusual $IFS value:
{
	# The first item without a preceding space character.
	read line
	printf '%s' "${line}"
	# Additional list items with a separating space character inserted.
	while read line
	do
		printf ' %s' "${line}"
	done
	# Terminating newline character.
	printf '\n'
} << 'EOT' > list
abc
xyz
EOT
I wonder why you go through the troubles of converting static data at runtime though. Or does your heredoc contain shell variables?​
 
Back
Top