bashzshglob

Bash (vs. Zsh): Globbing behavior differences in different environments


I'm trying to learn some bash basics and ran into some unexpected behavior when echoing out some strings which can include "*" characters. Now, I'm trying to understand when bash decides to glob outputs.

I can run type the following and execute it in bash:

foo="*"; echo $foo

Output: *

But then when I put the same code into a file, printFoo.sh, and execute:

#!/usr/bin/env bash
foo="*"; echo $foo

Output: printFoo.sh foo.txt bar.txt

How is the environment different when the command is run within a file that causes this difference in behavior?

My bash version is: GNU bash, version 3.2.57


Solution

  • bash does not behave differently with respect to globbing when used interactively or in a script. From your behaviour, I would rather conclude that your interactive shell is Zsh, since this is the only popular shell I am aware off which would behave in this way. Of course, if you wrote a zsh script, it would also not expand $foo in your example.