Search code examples
macosbashglob

Mac OS X - bash default pathname expansion or bug


I'm trying to run the following line of script in bash on Mac OS X 10.6.4 (from this question):

$ export EDITOR='mvim -f -c "au VimLeave * !open -a Terminal"'

Alas, what I get is something unexpected:

$ echo $EDITOR
mvim -f -c "au VimLeave Desktop Documents Downloads Library Movies Music Pictures Public Sites bin !open -a Terminal"

The expected output would be:

$ echo $EDITOR
mvim -f -c "au VimLeave * !open -a Terminal"

The way to fix this to this is to set noglob, i.e. run set -f immediately prior to the export assignment. However, the question at hand is whether this is the expected behaviour on Mac OS X because (because noglob is unset by default, i.e. set +f) or because there is a bug in bash on Mac OS X.

The version of bash is:

$ bash --version
GNU bash, version 3.2.48(1)-release (x86_64-apple-darwin10.0)
Copyright (C) 2007 Free Software Foundation, Inc.

There may be some assistance by way of page 329 of A practical guide to Unix for Mac OS X users: "Unless noglob (page 320) is set, the shell performs [pathname expansion] when it encounters an ambiguous file reference--a token containing any of the unquoted characters &, ?, [, or ].". However because the * being globbed is within quotes, the question remains: Is the behaviour a default setting of bash, or a bug?

This is just a curiosity, but I'd be grateful for any thoughts and input you may have.

Brian


Solution

  • Your EDITOR variable is set correctly. You can see this if you execute:

    echo "$EDITOR"
    

    Have a look at the following transcript:


    pax> export EDITOR='mvim -f -c "au VimLeave * !open -a Terminal"'
    
    pax> echo $EDITOR
    mvim -f -c "au VimLeave SecretCiaDoc.txt NsaEchelonKeys.txt !open -a Terminal"
    
    pax> echo "$EDITOR"
    mvim -f -c "au VimLeave * !open -a Terminal"
    

    Your problem lies not with the set statement but with your echo. The set will not expand the * because it's contained within single quotes but doing an echo without quotes will expand it.

    This in no way affects programs which use the environment variable.


    Based on your comment:

    This is still weird: that * is still within quotes (the double quotes) for the echo command. a='abc "*" xyz'; echo $a does not expand for me in either bash or dash; in fact, it includes the quotes as the second argument.

    Watch this:

    pax> a='abc "*" xyz' ; echo $a
    abc "*" xyz
    
    pax> a='abc "* xyz' ; echo $a
    abc "* xyz
    
    pax> a='abc " * xyz' ; echo $a
    abc " SecretCiaDoc.txt NsaEchelonKeys.txt xyz
    
    pax> touch '"hello' ; a='abc "* xyz' ; echo $a
    abc "hello xyz
    

    See what's happening. It's not treating the " as anything special, just another character. The reason why it expands for your EDITOR is because it's on its own. When you use "*", it's actually trying to expand files that begin and end with " - you can see that in my last example above.