-2

This code in bash read and process lines in a variable

while read -r line; do
    echo "... $line ..."
done <<< "$list"

If i replace <<< with <, it does not work. Wonder why is that

Justin Lin
  • 673
  • 1
  • 6
  • 15
  • 1
    See: [Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/Redirections.html) and [wiki.bash-hackers.org](http://wiki.bash-hackers.org/syntax/redirection). `<` redirects files. So whatever `$list` expands to, `bash` tries to redirect that as a file. That's why it doesn't work. – PesaThe Jan 12 '18 at 23:19
  • StackOverflow is about helping people fix their existing programming code. Requests for tutorials, research, tools, recommendations, libraries, and code are off-topic. ***Please*** read https://stackoverflow.com/help/on-topic , https://stackoverflow.com/help/how-to-ask , https://stackoverflow.com/help/dont-ask , https://stackoverflow.com/help/mcve and take the [tour](https://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Jan 13 '18 at 00:01
  • [What's the difference between <<, <<< and < < in bash?](https://askubuntu.com/questions/678915/whats-the-difference-between-and-in-bash) – Nick Dong Feb 21 '23 at 02:46

4 Answers4

5

< is used to redirect from a file. So < "$list" looks for a file whose name is the value of the list variable, and tries to read from that file.

<<< creates a here-string, <<< "$list" reads directly from the value of the variable.

Barmar
  • 741,623
  • 53
  • 500
  • 612
3

Both are forms of redirection. There are three standard I/O streams: standard input (stdin), standard output (stdout) and standard error (stderr). All are directed at the current terminal by default, but all can be redirected somewhere else.

With stdin used by the read command, the input will come from the keyboard by default but can come from a file (bearing in mind that "everything is a file").

In the case of < the input comes from a file which the user specifies (ignoring the extra complication of process substitution).

However there are two constructs that enable us to specify the data inside our script, these are << (a heredoc) and <<< (a here-string). These two "magically" redirect the data from the script into the command's stdin.

The magic is not particularly efficient. What happens under the covers is that a temporary file is created, the data is written to it, then that file is used for input.

So, the difference is:

< the user supplies the file

<<< the shell creates the file for you from the data supplied

(Reference: bash source code redir.c, esp. function write_here_string())

cdarke
  • 42,728
  • 8
  • 80
  • 84
2

Have a look on this page.

Redirecting input

N < SOURCE

The input descriptor N uses SOURCE as its data source. If N is omitted, filedescriptor 0 (stdin) is assumed.

And

<<TAG
...
TAG

A here-document is an input redirection using source data specified directly at the command line (or in the script), no "external" source. The redirection-operator << is used together with a tag TAG that's used to mark the end of input later:

# display help

cat <<EOF
Sorry...
No help available yet for $PROGRAM.
Hehe...
EOF

And

<<< WORD 

The here-strings are a variation of the here-documents. The word WORD is taken for the input redirection:

cat <<< "Hello world... $NAME is here..."

Just beware to quote the WORD if it contains spaces. Otherwise the rest will be given as normal parameters.

The here-string will append a newline (\n) to the data.

mju
  • 591
  • 1
  • 6
  • 17
0

from Bash man page:

   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 performed.  The result is supplied as a single string to the command on its standard input.

You can find out answers like this by consulting reference manuals, which in linux, can be easily accessed by man command.

Jason Hu
  • 6,239
  • 1
  • 20
  • 41