I would like to use bash process substitution for a sudo command.
For example, here\'s a non-sudo command that works for me:
$ cat <(echo \"installed.
sudo bash -c 'cat <(echo "installed.txt")'
The behaviour described in the question is due to the design of sudo
.
$ sudo cat <(echo "installed.txt")
cat: /dev/fd/63: Bad file descriptor
The error occurs because sudo
has a default behaviour which closes file descriptors except for standard input, output and error. As described on the man page:
By default, sudo will close all open file descriptors other than standard input, standard output and standard error when executing a command.
This behaviour can be overridden with the -C
(or --close-from
) option to specify a file descriptor number below which files should not be closed. However, use of this option must be permitted by an administrator: the following needs to be added to /etc/sudoers
Defaults closefrom_override
With that in place, the command will work if -C
is used:
$ sudo -C64 cat <(echo "installed.txt")
installed.txt
(here the number 64
was given because it was greater than the 63
in the error message).
The best approach is probably to put everything in a script, and run that script with sudo
.
Try doing this in your shell :
$ sudo bash -c 'cat <(echo "installed.txt for UID=$UID")'
installed.txt for UID=0