0

I am new with Bash, and I am extending a script made by third parts.

In the following code snippet:

while IFS='|' read my_var
do
    (
     # commands based on $my_var
    ) &
done < <(psql my_db "SELECT ...")

I do not undertstand what < < does.

If < < were <, I would say the snippet would do:

  • run the SELECT... query on my_db.

  • take the result of the query ( a string of elements separated by | ) and store time by time each element in my_var.

  • run in parallel the commands inside every while loop, using each time a different value for my_var.

Is this correct?
How does having < < instead of < change things ?

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Tms91
  • 3,456
  • 6
  • 40
  • 74
  • 1
    `<` is one piece of syntax. `<( )` is another. They do two different things, but can be usefully combined. `< <` is not a piece of meaningful syntax, but `<` is, and so is `<( )`. – Charles Duffy Feb 15 '22 at 17:47
  • To understand the _why_, see [BashFAQ #24](https://mywiki.wooledge.org/BashFAQ/024). – Charles Duffy Feb 15 '22 at 17:48
  • ...whereas to understand the `while read` in general, see [BashFAQ #1](https://mywiki.wooledge.org/BashFAQ/001). – Charles Duffy Feb 15 '22 at 17:49
  • 2
    (I'm working on finding better duplicates -- this is a frequently-asked question, but it's not easy to search for). – Charles Duffy Feb 15 '22 at 17:52
  • Thanks in advance @CharlesDuffy – Tms91 Feb 15 '22 at 17:56
  • "Read into shell variables from a pipe" added as an extra duplicate because it's effectively a local equivalent to BashFAQ #24, describing the problem that motivates use of this construct instead of piping into the loop. – Charles Duffy Feb 15 '22 at 17:59
  • ...for a more detailed description of `<( )`, btw, https://wiki.bash-hackers.org/syntax/expansion/proc_subst is an excellent resource. As you can see from `echo <(ls)`, `<( )` evaluates to a file; the other `<` redirects from that file. – Charles Duffy Feb 15 '22 at 18:01
  • hi @CharlesDuffy I just found out this question is also duplicate of https://stackoverflow.com/a/2443120/7658051 , that was pretty much what I was looking for but I could not find it since search engines return very different results for `done < <( ...)` – Tms91 Mar 07 '22 at 10:09
  • Thanks; added that one to the duplicate list. – Charles Duffy Mar 07 '22 at 12:45

0 Answers0