Why is Globbing Working Differently in Bash and Zsh?

0
1
Asked By CuriousCoder22 On

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.

1 Answer

Answered By CodeWizard99 On

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.

LogicLover7 -

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

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.