How to get asterisk '*' in zsh to have same behaviour as bash?

ls * would have the same effect in bash. No matter what the shell is, what happens is that the shell first expands the wildcards, and then passes the result of the expansion to the command. For example, suppose the current directory contains four entries: two subdirectories dir1 and dir2, and two regular files file1 and file2. Then the shell expands ls * to ls dir1 dir2 file1 file2. The ls command first lists the names of the arguments that are existing non-directories, then lists the contents of each directory in turn.

$ ls
dir1  dir2  file1  file2
$ ls -F
dir1/  dir2/  file1  file2
$ ls *
file1  file2

dir1:
…

dir2:
…

If ls behaved differently in bash, either you've changed the bash configuration to turn off wildcard expansions, which would turn it off everywhere, or you've changed the meaning of the ls command to suppress the listing of directories, probably with an alias. Specifically, having

alias ls='ls -d'

in your ~/.bashrc would have exactly the effect you describe. If that's what you did, you can copy this line to ~/.zshrc and you'll have the same effect.


The fact that rm -rf somepath/* has a different effect in bash and zsh when somepath is an empty directory is a completely different matter.

In bash, if somepath/* doesn't match any files, then bash leaves the wildcard pattern in the command, so rm sees the arguments -rf and somepath/*. rm tries to delete the file called * in the directory somepath, and since there's no such file, this attempt fails. Since you passed the option -f to rm, it doesn't complain about a missing file.

In zsh, by default, if a wildcard doesn't match any files, zsh treats this as an error. You can change the way zsh behaves by turning off the option nomatch:

setopt no_nomatch

I don't recommend this because having the shell tell you when a wildcard doesn't match is usually the preferable behavior on the command line. There's a much better way to tell zsh that in this case, an empty list is ok:

rm -rf somepath/*(N)

N is a glob qualifier that says to expand to an empty list if the wildcard doesn't match any file.