3

The following works in bsd unix, bash shell:

stuff=$(echo "dog cat rat")

read -r -a astuff <<< "$stuff"

for file in "${astuff[@]}"; do echo "file=$file"; done

Clearly the line containing <<< breaks $stuff into an array and puts the array into astuff. But I do not understand the syntax. Is there a man page or other documentation for <<<? And how does this break the string at spaces?

Aaron
  • 24,009
  • 2
  • 33
  • 57
Jacob Wegelin
  • 1,304
  • 11
  • 16
  • 1
    It's `read` that splits the string around spaces (more precisely, around characters of $IFS). – Aaron Oct 13 '16 at 14:48
  • 1
    `<<<` introduces a "here string" - search for that on [the man page](https://linux.die.net/man/1/bash). – cxw Oct 13 '16 at 14:49
  • [This](http://tldp.org/LDP/abs/html/x17837.html) is another good ressource on here strings. – Aaron Oct 13 '16 at 14:50
  • http://unix.stackexchange.com/questions/80362/what-does-mean might also help – zedfoxus Oct 13 '16 at 14:50
  • 1
    And the assignment is a classic [useless use of `echo`](http://www.iki.fi/era/unix/award.html#echo) – tripleee Oct 13 '16 at 16:40
  • (Aside: The reason I didn't mark-as-dupe myself is that this also was asking about the array-splitting, not found in the other question; the top-voted answer's failure to address that aspect is also why I added my own. But it was a decision that could have gone either way, and I don't feel compelled to reopen). – Charles Duffy Oct 13 '16 at 16:43

3 Answers3

4

It's called here strings. You can read here.

Here Strings A variant of here documents, the format is:

          [n]<<<word

   The word undergoes brace expansion, tilde expansion, parameter and
   variable expansion, command substitution, arithmetic expansion, and
   quote removal.  Pathname expansion and word splitting are not
   performed.  The result is supplied as a single string, with a newline
   appended, to the command on its standard input (or file descriptor n
   if n is specified).
P.P
  • 117,907
  • 20
  • 175
  • 238
  • Nice. A more direct link to an explanation: http://www.tldp.org/LDP/abs/html/x17837.html. – Mark A. Fitzgerald Oct 13 '16 at 14:51
  • 6
    Please don't link to the ABS. It's full of errors and bad practices. Here's a link to the [official documentation](https://www.gnu.org/software/bash/manual/bashref.html#Here-Strings) – chepner Oct 13 '16 at 14:54
  • 3
    @MarkA.Fitzgerald, ...if you want some better-regarded links than the ABS that are less formal than the official documentation, you might also consider [the bash-hackers wiki](http://wiki.bash-hackers.org/syntax/redirection#here_strings), or [the Wooledge wiki](http://mywiki.wooledge.org/HereDocument) (here-strings covered near the bottom of the page). The ABS is the w3schools of shell scripting -- infamous for spreading bad practices and outdated information. – Charles Duffy Oct 13 '16 at 16:05
  • Nice direct link, @chepner, and thanks for the heads-up regarding the quality of the ABS. – Mark A. Fitzgerald Oct 13 '16 at 18:25
  • @CharlesDuffy: nice analogy to w3schools re: the ABS - good to know. – Mark A. Fitzgerald Oct 13 '16 at 18:26
1

In

read -r -a astuff <<< "$stuff"

...<<< is "here-string" syntax, syntactic sugar providing a shorter way to write:

read -r -a astuff <<EOF
$stuff
EOF

read is thus the command responsible for assigning your content into astuff; the -a argument tells it to split on characters in IFS (by default, spaces, tabs and newlines) into an array.


References:

...and, on the read component:

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

You can search man bash for explanations. This is one possible way:

 $ man bash | grep -A10 -B4 "<<<"

It greps for "<<<" in the output of man bash, displaying beside the line containing "<<<" also 4 lines before that line, and 10 lines after. On my Debian I get this result:

   Here Strings
       A variant of here documents, the format is:

              <<<word

       The  word  undergoes  brace  expansion,  tilde expansion, parameter and
       variable expansion, command  substitution,  arithmetic  expansion,  and
       quote  removal.   Pathname  expansion  and  word splitting are not per‐
       formed.  The result is supplied as a single string to  the  command  on
       its standard input.

And 3 more lines, which are unrelated.

You can repeat this search for other words and "words" you don't understand.