[Bash-completion-devel] -o posix, IFS and local

Crestez Dan Leonard cdleonard at gmail.com
Fri Feb 5 23:55:24 UTC 2010


I've been working on a solid fix testable fix for the "spaces in mount
bug":
https://alioth.debian.org/tracker/index.php?func=detail&aid=311410&group_id=100114&atid=413095
I pushed the WIP code to a branch in the repo:
http://alioth.debian.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=bash-completion/bash-completion.git;a=blob;f=contrib/mount;h=41afb07df566f698f8952d510de09ba8f299763f;hb=b493869b8d4e5b2a1715d15c86dd3a3a01739299

I managed to get it to a testable state and automated tests revealed
that my code leaked variables. This a very very simple fix; just mark
the variables as local. Doing that unexpectedly broke the code by
leaking one-line IFS changes. Correcting that is also easy but I don't
understand why making a variable local triggers this. Here is the code
in question; the fs_* variables need to be marked as local:

while read -r fs_spec fs_file fs_other; do
    if [[ $fs_spec = [#]* ]]; then continue; fi
    [[ $fs_spec = */* ]] && { IFS=$'\0' \
eval "COMPREPLY+=( $'$fs_spec' )"; }
    [[ $fs_file = */* ]] && { IFS=$'\0' \
eval "COMPREPLY+=( $'$fs_file' )"; }
done

Some digging reveals that bash-completion tests run with the shell in -o
posix mode and that the IFS leak is correct behavior. Bash doc states in
6.11 (Bash POSIX mode) point 23:

""" Assignment statements preceding posix special builtins persist in
the shell environment after the builtin completes. """
http://www.gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode

And indeed; eval is a builtin. But this does not actually explain why
marking a variable as local has any effect. As far as I can tell the
code should *always* leak the IFS change. Whether the fs_* vars are
local or not should be irrelevant.

This behavior makes absolutely no sense to me. I even tried to reproduce
in a standalone script but -o posix behaved as advertised and IFS always
leaked.

I'm just going to skip this and simply save/restore the value of IFS.
But if anyone can make sense of this; please tell me.




More information about the Bash-completion-devel mailing list