Search code examples
bashprocess-substitution

How to prevent bash from returning the same path when doing multiple process substitution?


I want to use process substitution to pass some strings as files:

arg() {
    if true; then
        echo <(echo file)
    else
        echo inline
    fi
}

config() {
    echo config content
}

echo -arg $(arg) -config <(config)

-arg can accept either a file path or an inline value. So I created a function for it.

But the problem is that this script outputs

-arg /dev/fd/63 -config /dev/fd/63

Which is wrong, because then the two flags receive the same content.

Is it because arg was ran in a new shell?

I wonder how can I tell bash not to use the same path for multiple process substitutions?

I'm using bash 4.4.12


Solution

  • The problem is that returned value of <(..) must be used in the same command. The file descriptor is no more valid outside command context.

    In arg function a file descriptor 63 is open to read output of echo file command but it is not read, and no more valid after.