help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Process substitution documentation request


From: Borden
Subject: Re: Process substitution documentation request
Date: Sun, 14 Jun 2026 04:21:33 +0200 (CEST)

I appreciate the thoughtful response. The problem I'm trying to solve is:

$ sudo cryptsetup luksAddKey --key-file=<(gpg --decrypt "$EXISTING_KEY") 
--new-keyfile=<(gpg --decrypt "$NEW_KEYFILE") "$DEVICE" 
Simply: I want to pass two decrypted keys straight into cryptsetup an bypass 
the hard disk. I don't so much have a specific need as this is just how I want 
to do it. I don't think I'm unreasonable in trying to keep plain text keys off 
my hard disk entirely. I have working scripts that let me pipe one key into 
cryptsetup or pass two decrypted keys stored in files.

So far, it seems like this is theoretically possible, but it's failing (as you 
probably saw immediately) for reasons that I can only guess. I'm also learning 
that sudo throws another layer of complexity as it runs in a different user 
space that might not have access to the same unprivileged files.
I hoped the Manual had something explaining how <() opens, feeds, closes the 
pipe, and how it fits into the general parsing chain, so if something isn't 
working or is impossible, I know why. The Manual has helped me learn parameter 
expansion, for example, and is usually quite useful.

However, I agree that the Manual is not a computer science course. If I lack 
the foundational knowledge to understand asynchronous IO, then the Manual 
shouldn't teach. However, it would still be helpful nudging me into what I 
should learn before reattempting bash.

My endgame is to try to divert questions from your inbox by suggesting 
information that the Manual might want to add. I'm doubtful that my question is 
such a corner case that nobody will ever ask it again, which is why I'm 
confused that I don't see a similar answer already.
13 Jun 2026, 11:03 by greg@wooledge.org:

> On Sat, Jun 13, 2026 at 05:58:58 +0200, Borden via wrote:
>
>> I'm trying to figure out the mechanics of process substitution from 
>> https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html 
>> and the rest of the Internet.
>>
>> The bash manual doesn't need a lengthy explanation on asynchronous IO. It 
>> would be sufficient to explain the differences named pipes have from stream 
>> redirectors, generally explain how named pipes can be read and written, and 
>> what users should search for in their foundational research.
>>
>
> The guidance you're looking for is more appropriate for a book or a
> tutorial, rather than official documentation, I believe.
>
> Process substitutions work very much like anonymous pipes.  Two or more
> programs are running concurrently, and input may be available or
> unavailable based on whether it has already been written.
>
> Consider a basic pipeline like this:
>
>  slowwriter() { while sleep 1; do echo "$RANDOM"; done; }
>  eagerreader() { local REPLY; while read -r; do echo "Next: $REPLY"; done; }
>  slowwriter | eagerreader
>
> If you run this, you'll see a line of output every second, until you
> interrupt it (e.g. with Ctrl-C).
>
> We can do the same thing with a process substitution:
>
>  eagerreader < <(slowwriter)
>
> Once again, we see a line of output every second, until the commands are
> interrupted.
>
> So, what's the difference?  In this case, there is no real difference
> other than the way we would check exit codes, if we cared about those.
> But let's take a different example:
>
>  printf '%s\n' {1..10} | readarray -t numbers    # bash 4.0 or later
>
> This has no visible effect, because the readarray command takes place
> in a subshell.  The numbers array is populated, but that happens in
> a subshell, and the resulting array is lost when the subshell exits.
>
> But if we do it in a process substitution, the result is different:
>
>  readarray -t numbers < <(printf '%s\n' {1..10})
>
> In this case, the readarray command is *not* executed in a subshell.
> It's executed by our interactive shell, and therefore the numbers array
> survives.  We can see it with "declare -p numbers" (among others).
>
> This is the primary use of process substitutions in most scripts.
> There are of course many other uses, but I'm not going to try to list
> them all here.
>
> If you have more specific questions, feel free to ask.
>




reply via email to

[Prev in Thread] Current Thread [Next in Thread]