Z Shell

From Rosetta Code
Revision as of 22:19, 27 September 2016 by rosettacode>Micahwelf (Ad a little explanation about what makes Z Shell more and different from Unix Shell)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Z Shell is an implementation of UNIX Shell. Other implementations of UNIX Shell.

Zsh Home | Wikipedia: Z Shell | Reddit: Zsh

Z Shell is a POSIX-compatible shell, but with much more to offer. Script programs can be written in Z Shell, using modules and advanced variable and pattern handling, that rival compiled languages like C and that no standard POSIX shell could do. Following are some examples:

To replace the system 'sleep' command with a shell function that accepts floating point seconds, minutes, hours, days, or weeks:

function delay sleep {
  emulate -LR zsh -o extendedglob -o nullglob
  local -F 32 Delay
  if [[ $1 == (#b)(digit:(#c1,).(#c0,1)digit:(#c0,))(s|m|h|d|w|) ]]
    if [[ $match[2] == (s|) ]] Delay=$[ $match[1] ]
    if [[ $match[2] == (m) ]] Delay=$[ $match[1] * 60. ** 1 ]
    if [[ $match[2] == (h) ]] Delay=$[ $match[1] * 60. ** 2 ]
    if [[ $match[2] == (d) ]] Delay=$[ ($match[1] * 60. ** 2) * 24 ]
    if [[ $match[2] == (w) ]] Delay=$[ (($match[1] * 60. ** 2) * 24) * 7 ]
    : $(read -u 1 -t $Delay)
    print -u 2 "Invalid delay time: $1"
    return 1

To replace the system's "cat" command for standard input and output:

function zsh_cat {
  emulate -LR zsh
  zmodload zsh/system
  local buffer
  while sysread buffer
    syswrite $buffer

The zsh_cat function, in particular, is a good, basic example of what cannot be done in the popular bash/sh. Z Shell can do much more. Here are just a few more of Z Shell capabilities:

  • Open TCP ports on file-descriptors
  • Copy/move files with Zsh builtins
  • Do FTP transactions
  • Match filename patterns and string patterns with the Zsh-super-advanced glob-pattern matching, Regex, or PCRE regex.
  • Emulate other shells with great precision, making Zsh the only shell needed on a typical system:
    • SH/Bash
    • Csh
    • Ksh
  • Load and run pre-parsed/compiled word-code files, speeding up the execution of scripts and autoload functions. Also, this makes possible distributing a collection of pre-compiled autoload functions all as one .zwc file that can be likened to a plugin or an app that will run on any Zsh system (like a .Net exe or a Java jar).
  • Load new modules made for the current version of Zsh, like a native graphical front-end for "Zsh apps". (For graphical, it is probably better to just use an input-output interface to wish, the Tcl/Tk GUI)

With all Zsh can do, it is worthy of being considered a language of its own, separate from UNIX Shell but fully compatible. Though the executable for Zsh is much smaller than Bash, much of the extra capabilities, including Regex matching and new-user-detection load from modules and function autoload files. These numerous other files can be customized extensively, potentially creating system command/invocation incompatibilities. These can be overcome by using the standard option "-f", and by using the either the correct command name ("/bin/"(zsh|sh|ksh|bash)) or an emulation command ("emulate <shell> <shell-options>"). These features can ensure that the code written will be interpreted exactly how it is intended to be.