2

I'm trying to learn about here-docs and here-strings. I'd like to understand why the output of echo <<< more than one word omits the first word. E.g.

> echo <<< more than one word
than one word
> echo <<< a b c
b c
>echo <<< "more than one word"
 -- <blank line> --

The same behavior is not true of cat:

>cat <<< "more than one word"
more than one word

What I have researched:

The Wikipedia Here document article. Frankly I tend to find many Wikipedia articles abstruse, including the noted; plus their examples did not address this specific issue.

My distro:

>cat /proc/version 
Linux version 3.11.10-301.fc20.x86_64 (mockbuild@bkernel01.phx2.fedoraproject.org) (gcc version 4.8.2 20131017 (Red Hat 4.8.2-1) (GCC) ) #1 SMP Thu Dec 5 14:01:17 UTC 2013

I also could not find an existing question on Super User on this specific topic.

  • What distro are you running? – Silas B Mar 7 '17 at 20:40
  • Updated post with my distro. – StoneThrow Mar 7 '17 at 20:46
1

<<< feeds left-hand command stdin with right counterpart.

This has a meaning for the cat command, which prints its stdin when no file name is given as argument. But this has no meaning for echo which always ignores its stdin and only prints its arguments.

0

I've read this other answer few times and it wasn't clear to me, until I realized that <<< sees only a on its right side and sends it to the command stdin. This means

echo <<< a b c

is equivalent to

echo b c <<< a

or even

<<< a echo b c

I.e. b and c are arguments to echo even if they are after <<< a (compare the equivalence of cat -n < foo.txt, cat < foo.txt -n, < foo.txt cat -n).

Now the statement

echo […] always ignores its stdin and only prints its arguments

fulfills the explanation.

-1

Good answer here

The string that you are typing after the '<<<' is a text delimiter. You are using three '<' instead of 2, which may be causing the interesting behaviour you are noting. If you use the command cat instead of echo, the behaviour will be more what you expect.

Basically the words 'more' and 'a' are being used as flags to identify when the multiline string has ended.

  • -1. The link is useful but the answer seems to confuse here string (<<< syntax) and pure here document (<<). Text delimiter you mention is a part of here document syntax, it doesn't apply to the case the question is about. – Kamil Maciorowski Jul 6 '17 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.