Examine key parts of the Z shell (zsh) and how to use its features to ease your UNIX system administration tasks. Z shell is a popular alternative to the original Bourne and Korn shells. It provides an impressive range of additional functionality, including improvements for completing different commands, files, and paths automatically, and for binding keys to functions and operations.
After reading this article, I decided to learn some more about the features of zsh. Maybe it’s really worth using. I have to admit that I usually use csh for CLI operations and sh for scripting, because it’s available almost everywhere and I don’t use any of the newer features of bash, so I can rely on scripts running on each machine having sh installed (usually it’s the default shell, at least for scripting and for system maintenance).
Furthermore, some of the “easier” features of zsh can be achieved using csh, too. Especially the use of selection [] can be tone with csh:
zsh$ ls ^*.c
% ls *.[^c]
zsh$ ps -ax |fgrep -f =(print -l httpd imapd)
% ps ax | awk ‘/httpd/ /imapd/’
It’s just more typing, in most cases. 🙂
But group expressions, command (!) completition (rather than filename completition) and multiple redirection are quite cool. The “harder” features of zsh can be implemented with other shells as well, but it seems to be very easy with zsh.
The section “File and command completion” explains how the TAB key is used for file name completition; I think the completition mechanism of the csh works better because you don’t need to press TAB a second time to get the alternatives (at actual completition point) listed, it’s done immediately. If TAB is pressed, the input is completed until the point where it is definite, and then the upcoming alternatves are shown. It also works for directory contents. Let me give an example:
% joe l-TAB-
lernhilfen/ logbuch/ lohn/
% joe log-TAB-
% joe logbuch/
% joe logbuch/-TAB-
% joe logbuch/l
land.tex log_hoch.tex log_quer.tex
% joe logbuch/la-TAB-
logbuch/land.tex
% joe logbuch/land.tex -RETURN-
… now running joe logbuch/land.tex …
It’s very useful to feel one’s way into a directory structure if the content is not known by name.
An additional note: The section “Multiple I/O streams” refers to non-integrated commands such as paste, cut, sort and grep; these are not elements of zsh, they belong to the OS’s basic tools. But I think that should be obvious.
PS. Would be great to have HTML (pre)…(/pre) environment available.
ZSH can TAB Complete almost everything, for example can complete kldload/kldunload kernel module, or TAB complete files to extract from tar archive, like You would be doing TAb completion cd to another directory. Can also TAB -OPTION for command, (ls -[TAB]).
ZSH is the most powerfull/comfortable shell ever.
The same works for bash shell too. It has a configurable completion support and if you install a package like bash-completion you’ll have options for many programs ready.
e.g:
# scp -[TAB]
# scp -r se[TAB]
# scp -r server:/e[TAB]
# scp -r server:/etc/hos[TAB]
# scp -r server:/etc/hosts /etc
And some other features (like process substitution, and few of the filename matching rules) are also available.
I guess they’re probably comparing zsh to original bsh, not currently available bash. (Yet it does not make zsh less interesting, probably bash has inspired those feature looking at zsh).
does BASH also can TAB complete inside names like that:
% ls
misc/ resolution scripts/ tmp/
% vi sol[TAB]
Completes to:
% vi resolution
Not only do I have zsh completing within names, it also corrects capitalization
% ls -1 *[Dd]ev*
Edge Device Summary.xls
devcom
% soffice devi[TAB]
Competes to the file I want:
“Edge Device Summary.xls”
If I had left it at “dev[TAB]” devcom would have been chosen since it actually matches my case
looping
Another thing I find helpful in interactive use is the use of ellipses:
for i in {1..100000}; do rm file${i}; done
padding: files rigidly named file###
for i in {001..300}; do rm file${i}; done
Want to repeat a process many times?
repeat 1000 echo hello
tab complete ssh
add the following to your zshrc, and you can tab complete the hosts found in ssh’s known_hosts. If you have not done so already, you may have to run compinstall
compinit
hosts=(${${${${(f)”$(<$HOME/.ssh/known_hosts)”}:#[0-9]*}%% *}%%,*} )
zstyle ‘:completion:*:hosts’ hosts $hosts
This sounds interesting. I’m going to try these tricks out on the Windows port of ZSH.
I used zsh for quite a while, but when the time came to migrate to unicode, zsh simply hadn’t kept up. It pained me to leave the rather powerful completion possibilities behind (I particularly liked the menu-like navigation of completions that is available), as well as the more powerful globbing when compared with bash.
On the other hand, after going back to bash again, I found that the bash-completion in gentoo’s portage did most of the things I wanted completion for, and that the menu-based completion was hell compared to the bash way over a low-bandwidth SSH link. Also, bash had matured to version 3.x and now contained many of the features I switched to zsh to get.
I would, however, be extremely tempted to switch to zsh once again if unicode support was integrated properly.