Why is Globbing Working Differently in Bash and Zsh?

April 24, 2025 1 answer
Asked by tube_8strip
I'm noticing a difference in how Bash and Zsh handle globbing expansion in a simple script. Here's my code snippet: #! /bin/zsh while read lin do echo DEBUG line $lin done << EOJ foo * bar EOJ In Zsh, I correctly get `DEBUG line foo * bar`, but in Bash, the asterisk gets expanded to the files in the current directory. I'm trying to find out what Bash setting might be causing this double expansion and how I can make it behave like Zsh. My `.bashrc` doesn't have any specific settings for glob or expansion, so I'm curious if this is a default setting in Bash on Ubuntu. Also, I want to ensure my input data isn't expanded unless I specifically use `eval` or `$()`, as that could be a security issue.

Answers

!small correction~
You're right, Bash will expand the asterisk because it's not in double quotes. This is standard behavior for POSIX shells, while Zsh doesn't do this by default. To prevent globbing, just wrap the variable in double quotes when you echo it, like this: `echo DEBUG line "$lin"`. This will keep your input intact without expanding it to the file list.

_borrow a ladder: Thanks for that tip! Is there any way I can still allow field splitting without doing globbing?

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions