substitution "read all from fd" silently fails: $(<

2015-07-01 Thread Ilya Basin
Hi list.

Want to read whole stdin into variable.
Don't want to spawn new processes (cat).
Don't want to reopen existing fd &0

First thing I tried: $(<&0)
It silently returns an empty string.

From bash manual:
The command substitution $(cat file) can be replaced by the equivalent but 
faster $(< file).


reopening /dev/stdin mostly works, but easily broken by sudo:

# same user. works
[root@okdistr ~]# echo aaa | bash -c 'echo $(

Re: substitution "read all from fd" silently fails: $(<

2015-07-01 Thread Greg Wooledge
On Wed, Jul 01, 2015 at 10:19:10PM +0300, Ilya Basin wrote:
> Want to read whole stdin into variable.
> Don't want to spawn new processes (cat).

If the data stream does not contain any NUL bytes, you can use:

IFS= read -r -d '' variable



Re: substitution "read all from fd" silently fails: $(<

2015-07-01 Thread Geir Hauge
On Wed, Jul 01, 2015 at 10:19:10PM +0300, Ilya Basin wrote:
> Hi list.
> 
> Want to read whole stdin into variable.
> Don't want to spawn new processes (cat).
> Don't want to reopen existing fd &0
> 
> First thing I tried: $(<&0)
> It silently returns an empty string.

This type of query is preferably asked at the help-bash list rather than
bug-bash.

 
> # works, but too complex
> [root@okdistr ~]# echo aaa | sudo -u nobody bash -c 'a=; while true; do 
> rc=0; read -N1024 b || rc=$?; a=$a$b; [ $rc = 0 ] || break; done; echo "$a"'
> aaa

IFS= read -rd '' a
printf %s "$a"



Re: substitution "read all from fd" silently fails: $(<

2015-07-01 Thread Stephane Chazelas
2015-07-01 22:19:10 +0300, Ilya Basin:
> Hi list.
> 
> Want to read whole stdin into variable.
> Don't want to spawn new processes (cat).
[...]

Note that

$(&3), it doesn't work in ksh either, (in ksh, like in bash,
$(

Re[2]: substitution "read all from fd" silently fails: $(<

2015-07-01 Thread Ilya Basin
SC> 2015-07-01 22:19:10 +0300, Ilya Basin:
>> Hi list.
>> 
>> Want to read whole stdin into variable.
>> Don't want to spawn new processes (cat).
SC> [...]

SC> Note that

SC> $( execute /bin/cat in that process, it does the reading (from
SC> file) and writing (to the pipe) by itself (and the parent reads
SC> from the other end of the pipe to make-up the substitution).

Very informative, thanks.


-- 




Re: substitution "read all from fd" silently fails: $(<

2015-07-01 Thread Pádraig Brady
On 01/07/15 22:48, Stephane Chazelas wrote:
> 2015-07-01 22:19:10 +0300, Ilya Basin:
>> Hi list.
>>
>> Want to read whole stdin into variable.
>> Don't want to spawn new processes (cat).
> [...]
> 
> Note that
> 
> $( execute /bin/cat in that process, it does the reading (from
> file) and writing (to the pipe) by itself (and the parent reads
> from the other end of the pipe to make-up the substitution).
> 
> ksh (ksh93 and mksh) and zsh do not spawn a process in the
> $(