Re: {varname} redirection for a command or group leaves the file open

2017-05-11 Thread Aldo Davide
On Wed, May 10, 2017 at 8:52 PM, Pierre Gaston  wrote:

> See:
> https://lists.gnu.org/archive/html/bug-bash/2012-11/msg00040.html[https://lists.gnu.org/archive/html/bug-bash/2012-11/msg00040.html]
> Pierre
> PS: I'm with you ;)

Thanks for the link, I didn't find it in my earlier searches.

Well, that's disappointing. So there is no technical reason for this behavior 
other than copying the behavior of ksh. BTW zsh does the right thing and in the 
following scenario:

ls -lh /proc/self/fd {fd}>/dev/null

and it closes the file descriptor after the command has completed.



{varname} redirection for a command or group leaves the file open

2017-05-10 Thread Aldo Davide
Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: x86_64-pc-linux-gnu-gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' 
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-pc-linux-gnu' 
-DCONF_VENDOR='pc' -DLOCALEDIR='/usr/share/locale' -DPACKAGE='bash' -DSHELL 
-DHAVE_CONFIG_H   -I. -I./include -I. -I./include -I./lib  
-DDEFAULT_PATH_VALUE='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
 -DSTANDARD_UTILS_PATH='/bin:/usr/bin:/sbin:/usr/sbin' 
-DSYS_BASHRC='/etc/bash/bashrc' -DSYS_BASH_LOGOUT='/etc/bash/bash_logout' 
-DNON_INTERACTIVE_LOGIN_SHELLS -DSSH_SOURCE_BASHRC -O2 -march=native -pipe 
-Wno-parentheses -Wno-format-security
uname output: Linux mycomputer 4.9.24 #8 SMP PREEMPT Tue Apr 25 11:19:58 EEST 
2017 x86_64 Intel(R) Core(TM) i7-2670QM CPU @ 2.20GHz GenuineIntel GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 4.4
Patch Level: 12
Release Status: release

Suppose that you use a "varname" redirection when executing a simple 
command, e.g.:

ls -lh /proc/self/fd {var}>/dev/null

I was surprised to discover that the file descriptor remains open after 
the command has completed, as evidenced by issuing the following (works 
on linux) immediately afterwards:

echo "var is $var"
ls -lh /proc/$$/fd

This is unlike what happens with standard redirections, e.g.:

ls -lh /proc/self/fd 57>/dev/null
ls -lh /proc/$$/fd

The same problem exists when braces are used to group (possibly) 
multiple commands:

{ ls -lh /proc/self/fd; } {var}>/dev/null
echo "var is $var"
ls -lh /proc/$$/fd

On the other hand, everything works just fine with subshells:

(ls -lh /proc/self/fd) {var}>/dev/null
echo "var is $var"
ls -lh /proc/$$/fd

As a side-note, in the subshell example the variable var will be 
undefined in the second line, but defined inside the subshell. With 
groups it will remain defined after the group:

{ echo "inside the grouping: $var1"; } {var1}>/dev/null
echo "outside the grouping: $var1"
(echo "inside the subshell: $var2") {var2}>/dev/null
echo "outside the subshells: $var2"

So in summary, I would expect groups to work like subshells, both in 
regards to closing the file descriptor but also in regards to the scope 
of the variable.