Skip to main content
edited body
Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k

Easiest is to do:

{ rest of your script } | gpg... 

With shells compliant to the 2024 editingedition of the POSIX specification, you can do:

set -o pipefail { rest of your script } | gpg... 

To make sure that failure of rest of your script or gpg is not lost in the overall exit status.


exec | cmd runs 2 commands in parallel in 2 separate processes, the first one (evaluated in a child process in all shells) doing nothing without forking (exec should really have been called nofork), and with its stdout redirected to a pipe and the second (which in some shells is not done in a child) runs cmd (in a child if its external and therefore needs to be executed) with its stdin connected to the other end of a pipe.

In some shells, you can do:

exec > >(cmd) 

Using the (non-POSIX) process substitution feature from ksh Which runs cmd in background with its stdin connected to a pipe, and >(cmd) expands to the name of a file (which can be /dev/fd/x or /proc/self/fd/x or a named pipe depending on the system and shell) which once open for writing (as > does) gets you the other end of the pipe.

The exit status of cmd is lost in that case.

That you can do by hand by using a temporary named pipe:

mkfifo fifo || exit cmd < fifo & cmd_pid=$! exec > fifo rest of the script ret=$? exec >&- wait "$cmd_pid" || ret=$? rm -f fifo exit "$ret" 

(not handling the cleanup of the fifo if the script is killed).

Bearing in mind that there's still no command in POSIX (other than m4) to give you a unique temp file reliably, let alone one of type fifo.

Easiest is to do:

{ rest of your script } | gpg... 

With shells compliant to the 2024 editing of the POSIX specification, you can do:

set -o pipefail { rest of your script } | gpg... 

To make sure that failure of rest of your script or gpg is not lost in the overall exit status.


exec | cmd runs 2 commands in parallel in 2 separate processes, the first one (evaluated in a child process in all shells) doing nothing without forking (exec should really have been called nofork), and with its stdout redirected to a pipe and the second (which in some shells is not done in a child) runs cmd (in a child if its external and therefore needs to be executed) with its stdin connected to the other end of a pipe.

In some shells, you can do:

exec > >(cmd) 

Using the (non-POSIX) process substitution feature from ksh Which runs cmd in background with its stdin connected to a pipe, and >(cmd) expands to the name of a file (which can be /dev/fd/x or /proc/self/fd/x or a named pipe depending on the system and shell) which once open for writing (as > does) gets you the other end of the pipe.

The exit status of cmd is lost in that case.

That you can do by hand by using a temporary named pipe:

mkfifo fifo || exit cmd < fifo & cmd_pid=$! exec > fifo rest of the script ret=$? exec >&- wait "$cmd_pid" || ret=$? rm -f fifo exit "$ret" 

(not handling the cleanup of the fifo if the script is killed).

Bearing in mind that there's still no command in POSIX (other than m4) to give you a unique temp file reliably, let alone one of type fifo.

Easiest is to do:

{ rest of your script } | gpg... 

With shells compliant to the 2024 edition of the POSIX specification, you can do:

set -o pipefail { rest of your script } | gpg... 

To make sure that failure of rest of your script or gpg is not lost in the overall exit status.


exec | cmd runs 2 commands in parallel in 2 separate processes, the first one (evaluated in a child process in all shells) doing nothing without forking (exec should really have been called nofork), and with its stdout redirected to a pipe and the second (which in some shells is not done in a child) runs cmd (in a child if its external and therefore needs to be executed) with its stdin connected to the other end of a pipe.

In some shells, you can do:

exec > >(cmd) 

Using the (non-POSIX) process substitution feature from ksh Which runs cmd in background with its stdin connected to a pipe, and >(cmd) expands to the name of a file (which can be /dev/fd/x or /proc/self/fd/x or a named pipe depending on the system and shell) which once open for writing (as > does) gets you the other end of the pipe.

The exit status of cmd is lost in that case.

That you can do by hand by using a temporary named pipe:

mkfifo fifo || exit cmd < fifo & cmd_pid=$! exec > fifo rest of the script ret=$? exec >&- wait "$cmd_pid" || ret=$? rm -f fifo exit "$ret" 

(not handling the cleanup of the fifo if the script is killed).

Bearing in mind that there's still no command in POSIX (other than m4) to give you a unique temp file reliably, let alone one of type fifo.

added 1387 characters in body
Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k

Easiest is to do:

{ rest of your script } | gpg... 

With shells compliant to the 2024 editing of the POSIX specification, you can do:

set -o pipefail { rest of your script } | gpg... 

To make sure that failure of rest of your script or gpg is not lost in the overall exit status.


exec | cmd runs 2 commands in parallel in 2 separate processes, the first one (evaluated in a child process in all shells) doing nothing without forking (exec should really have been called nofork), and with its stdout redirected to a pipe and the second (which in some shells is not done in a child) runs cmd (in a child if its external and therefore needs to be executed) with its stdin connected to the other end of a pipe.

In some shells, you can do:

exec > >(cmd) 

Using the (non-POSIX) process substitution feature from ksh Which runs cmd in background with its stdin connected to a pipe, and >(cmd) expands to the name of a file (which can be /dev/fd/x or /proc/self/fd/x or a named pipe depending on the system and shell) which once open for writing (as > does) gets you the other end of the pipe.

The exit status of cmd is lost in that case.

That you can do by hand by using a temporary named pipe:

mkfifo fifo || exit cmd < fifo & cmd_pid=$! exec > fifo rest of the script ret=$? exec >&- wait "$cmd_pid" || ret=$? rm -f fifo exit "$ret" 

(not handling the cleanup of the fifo if the script is killed).

Bearing in mind that there's still no command in POSIX (other than m4) to give you a unique temp file reliably, let alone one of type fifo.

Easiest is to do:

{ rest of your script } | gpg... 

With shells compliant to the 2024 editing of the POSIX specification, you can do:

set -o pipefail { rest of your script } | gpg... 

To make sure that failure of rest of your script or gpg is not lost in the overall exit status.

Easiest is to do:

{ rest of your script } | gpg... 

With shells compliant to the 2024 editing of the POSIX specification, you can do:

set -o pipefail { rest of your script } | gpg... 

To make sure that failure of rest of your script or gpg is not lost in the overall exit status.


exec | cmd runs 2 commands in parallel in 2 separate processes, the first one (evaluated in a child process in all shells) doing nothing without forking (exec should really have been called nofork), and with its stdout redirected to a pipe and the second (which in some shells is not done in a child) runs cmd (in a child if its external and therefore needs to be executed) with its stdin connected to the other end of a pipe.

In some shells, you can do:

exec > >(cmd) 

Using the (non-POSIX) process substitution feature from ksh Which runs cmd in background with its stdin connected to a pipe, and >(cmd) expands to the name of a file (which can be /dev/fd/x or /proc/self/fd/x or a named pipe depending on the system and shell) which once open for writing (as > does) gets you the other end of the pipe.

The exit status of cmd is lost in that case.

That you can do by hand by using a temporary named pipe:

mkfifo fifo || exit cmd < fifo & cmd_pid=$! exec > fifo rest of the script ret=$? exec >&- wait "$cmd_pid" || ret=$? rm -f fifo exit "$ret" 

(not handling the cleanup of the fifo if the script is killed).

Bearing in mind that there's still no command in POSIX (other than m4) to give you a unique temp file reliably, let alone one of type fifo.

Source Link
Stéphane Chazelas
  • 587.9k
  • 96
  • 1.1k
  • 1.7k

Easiest is to do:

{ rest of your script } | gpg... 

With shells compliant to the 2024 editing of the POSIX specification, you can do:

set -o pipefail { rest of your script } | gpg... 

To make sure that failure of rest of your script or gpg is not lost in the overall exit status.