bug-bash
[Top][All Lists]
Advanced

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

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


From: Ilya Basin
Subject: substitution "read all from fd" silently fails: $(<&0)
Date: Wed, 1 Jul 2015 22:19:10 +0300

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
    address@hidden ~]# echo aaa | bash -c 'echo $(</dev/stdin)'
    aaa
    
    # different user. fail
    address@hidden ~]# echo aaa | sudo -u nobody bash -c 'echo $(</dev/stdin)'
    bash: /dev/stdin: Permission denied
    
    # spawn new process. not want
    address@hidden ~]# echo aaa | sudo -u nobody bash -c 'echo $(cat)'
    aaa
    
    # try to read from fd: silently fails
    address@hidden ~]# echo aaa | sudo -u nobody bash -c 'echo $(<&0)'
    
    # works, but too complex
    address@hidden ~]# 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
    
    address@hidden ~]#
    




reply via email to

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