Search code examples
bashprocess-substitution

BASH: Process Substitution in a Variable


What works is

vimdiff -c 'set diffopt+=iwhite' <(curl -sS '...') <(curl -sS '...')

Now I want to make a script that allows defining between two or four URLs and then generate the above command.

What I have for now:

#!/bin/bash

args=(-c 'set diffopt+=iwhite')
while [ -n "$1" ]; do
  args+=(<(curl -sS "$1"))
  shift
done

vimdiff "${args[@]}"

When run with two URLs it results in vimdiff showing two tabs without content and with the same filename at the bottom, e.g. "/proc/20228/fd/63". In other words the process substitution doesn't seem to be applied and/or wrong as I'd at least expect to see two different file descriptors in the filenames.

I guess that the problem is either in the args+=... line or in the last line.


Solution

  • This will do the trick, and it is very close to your original proposal:

    #!/bin/bash
    
    args=(-c 'set diffopt+=iwhite')
    while [ -n "$1" ]; do
      exec {fdnum}< <(curl -sS "$1")
      args+=(/dev/fd/$fdnum)
      shift
    done
    
    vimdiff "${args[@]}"
    

    So you'd first create all file descriptors via process substitution. By accessing their content via /dev/fd, you can easily collect them in an array and pass that to vimdiff.