Trying the following code with a Kornshell on Linux
-a exec 4>&1
-a echo hello | tee /dev/fd/4
produces the error
-a tee: /dev/fd/4: No such file or directory
Above test code works fine with Bash or Dash, though.
Any idea what's the problem with Ksh here? - Thanks.
Janis
On 28/07/2026 08:27, Janis Papanagnou wrote:
Trying the following code with a Kornshell on Linux
exec 4>&1
echo hello | tee /dev/fd/4
produces the error
tee: /dev/fd/4: No such file or directory
Above test code works fine with Bash or Dash, though.
Any idea what's the problem with Ksh here? - Thanks.
Janis
I'm not sure, but I think that /dev/fd/4 is really /proc/self/fd/4 and
the pipe is a new process - so a different 'self'.
On 28/07/2026 08:27, Janis Papanagnou wrote:
Trying the following code with a Kornshell on Linux
-a-a exec 4>&1
-a-a echo hello | tee /dev/fd/4
produces the error
-a-a tee: /dev/fd/4: No such file or directory
Above test code works fine with Bash or Dash, though.
Any idea what's the problem with Ksh here? - Thanks.
I'm not sure, but I think that /dev/fd/4 is really /proc/self/fd/4 and
the pipe is a new process - so a different 'self'.
Yes. The (obvious) workaround is to use /proc/$$/fd/4 instead (Yes, I just >tested this under ksh).
In article <1149qfh$3t4ai$1@dont-email.me>,
Richard Harnden <nospam.harnden@invalid.com> wrote:
On 28/07/2026 08:27, Janis Papanagnou wrote:
Trying the following code with a Kornshell on Linux
exec 4>&1
echo hello | tee /dev/fd/4
produces the error
tee: /dev/fd/4: No such file or directory
Above test code works fine with Bash or Dash, though.
Any idea what's the problem with Ksh here? - Thanks.
Janis
I'm not sure, but I think that /dev/fd/4 is really /proc/self/fd/4 and
the pipe is a new process - so a different 'self'.
Yes. The (obvious) workaround is to use /proc/$$/fd/4 instead (Yes, I just tested this under ksh).
Of course, the interesting part of this is: Why the difference between the shells? You'd have to dig deep into the man pages (and/or the source code) to figure it out, but it seems that bash/dash runs the last command in a pipeline in the parent shell and all the others in subshells, but ksh does
it the other way.
I have no idea whether the standard(s) requires or
prefers either option over the other.
And yet, the "lastpipe" option (See: man bash) is off by default, which
would seem to imply that, by default, the behavior described above does
*not* apply to bash. Hmmm...
Trying the following code with a Kornshell on Linux
exec 4>&1
echo hello | tee /dev/fd/4
produces the error
tee: /dev/fd/4: No such file or directory
Above test code works fine with Bash or Dash, though.
Any idea what's the problem with Ksh here? - Thanks.
Janis Papanagnou wrote:
Trying the following code with a Kornshell on Linux
exec 4>&1
echo hello | tee /dev/fd/4
produces the error
tee: /dev/fd/4: No such file or directory
Above test code works fine with Bash or Dash, though.
Any idea what's the problem with Ksh here? - Thanks.
Ksh sets the close-on-exec flag for fds > 2 opened with exec. This is explicitly allowed by POSIX. You can force the fd to remain open by
duping it to itself (although this is currently non-standard):
$ ksh -c 'exec 4>&1; echo hello | tee /dev/fd/4'
tee: /dev/fd/4: No such file or directory
hello
$ ksh -c 'exec 4>&1; echo hello | tee /dev/fd/4 4>&4'
hello
hello
This came up last year in the Austin Group. It was a side-discussion
that came out of another issue, but part of the resolution is that the
above work-around will be added to the standard in POSIX.1-2024 TC1.
See https://austingroupbugs.net/view.php?id=1913#c7300 which includes
the following addition to the text for n>&word and n<&word:
If word and n evaluate to the same open file descriptor, the
operation is a no-op except in shells which set the close-on-exec
flag for file descriptors greater than 2 opened using exec. In
these shells, a redirection of this form can be used to clear the
close-on-exec flag so that the file descriptor will remain open
when executing a non-built-in utility.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 74 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 50:07:10 |
| Calls: | 1,100 |
| Files: | 1,339 |
| Messages: | 275,859 |