[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Backticked, nested command will steal piped stdin
From: |
Dale R. Worley |
Subject: |
Re: Backticked, nested command will steal piped stdin |
Date: |
Tue, 30 Jul 2024 14:06:21 -0400 |
Justus <justus.kohl@gmail.com> writes:
> I found a bug in my beloved bash and hope that you can fix it!
> $ seq 3 | head -n $(calc -dp "1+1")
This actually isn't a bug; a command substitution inherits the fd's
except for 1 (stdout) (which is captured by bash to create the command
substitution).
Command substitution allows the output of a command to replace the comâ
mand name. There are two forms:
$(command)
or
`command`
Bash performs the expansion by executing command in a subshell environâ
ment and replacing the command substitution with the standard output of
the command, with any trailing newlines deleted.
Though I don't know exactly how command substitution interacts with the
redirections that are applied to the containing command.
In particular, I've been bitten occasionally by the construct
$ (command) | while read FOO BAR BAZ ; do (commands) ; done
when one of (commands) sucks on stdin.
In your case, you probably want
> $ seq 3 | head -n $(</dev/null calc -dp "1+1")
Dale