bashmacosvariablesglobquoting

macOS bash globbing quoted variable unexpectedly


On Linux with bash 5.2.15, single quotes prevent globbing as expected:

Good

$ dir='/path?/to?/file?'; echo $dir
/path?/to?/file?
$

However, on macOS with bash 3.2.57, I get this undesirable behavior:

Bad

$ dir='/path?/to?/file?'; echo $dir

$

This unintended globbing appears to occur in the echo command, as the variable is set correctly:

$ set | grep ^dir
dir='/path?/to?/file?'

I guess this unintended globbing is a bug in the ancient bash version on macOS. I tried setting noglob to no avail. Can you think of a workaround to get the Good behavior above?


Solution

  • Quoting the variable in the echo works:

    $ dir=/path?/to?/file?; echo "$dir"
    /path?/to?/file?
    

    As it turns out, quotes aren't needed when setting the variable.

    This now begs the question as to why the unquoted echo works on Linux without quoting.