🎓 A1Learner
🔍
0 of 50 lessons completed

Shell Zsh Differences

Zsh is a shell like bash, with a few behaviours of its own. This lesson shows the real differences, each one tried in zsh.

Zsh finds files at any depth

In zsh, ** matches folders at any depth. It finds text files in every folder below.

print **/*.txt

Bash needs globstar

Plain bash treats ** like *. Turn on globstar and bash matches at any depth too.

echo **/*.txt
shopt -s globstar
echo **/*.txt

print prints text

print is the zsh way to print a line, like echo in bash.

print "Hello from zsh"

Find a command with =

=ls expands to the full path of ls.

print =ls

Pick plain files only

*(.) is a glob qualifier. It keeps plain files and drops folders.

print docs/*(.)
print photos/*(.)

setopt changes settings

setopt turns a zsh setting on. It is the zsh twin of shopt in bash.

setopt extendedglob
setopt | grep extendedglob

Completion with Tab

Type part of a name and press Tab. Zsh completes it and can list the matches.

ls docs/

Example

print **/*.txt
print photos/*(.)

Key points

  • Glob: a pattern with * or ? that matches file names.
  • In zsh, ** matches files in this folder and in every folder below it.
  • In bash, ** needs shopt -s globstar to work the same way.
  • print: the zsh command that prints text, like echo.
  • =cmd: a zsh shortcut that expands to the full path of a command.
  • Glob qualifier: a test in brackets after a pattern, as in *(.) for plain files.
  • setopt: the zsh command that changes zsh settings, like shopt in bash.

In this example, ** finds every text file below this folder. Then the qualifier *(.) lists only the plain files in photos.

Note

Zsh completion suggests file and command names when you press Tab.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

In zsh, what does **/*.txt match?