16. Shell Parameters — Zsh 5.9.999.3-test documentation

# 16. Shell Parameters

The shell has many predefined parameters that may be accessed. Here are some examples:

Example 16.1: Job and status parameters

    % sleep 10 &
    [1] 3820
    % echo $!
    3820
    % set a b c
    % echo $#
    3
    % echo $ARGC
    3
    % ( exit 20 ) ; echo $?
    20
    % false; echo $status
    1

(`$?` and `$status` are equivalent.)

Example 16.2: Host UID and directories

    % echo $HOST $HOSTTYPE
    dendrite sun4
    % echo $UID $GID
    701 60
    % cd /tmp
    % cd /home
    % echo $PWD $OLDPWD
    /home /tmp
    % ls $OLDPWD/.getwd
    /tmp/.getwd

`~+` and `~-` are short for `$PWD` and `$OLDPWD`, respectively.

Example 16.3: Tilde shortcuts and misc

    % ls ~-/.getwd
    /tmp/.getwd
    % ls -d ~+/learning
    /home/learning
    % echo $RANDOM
    4880
    % echo $RANDOM
    11785
    % echo $RANDOM
    2062
    % echo $TTY
    /dev/ttyp4
    % echo $VERSION
    zsh v2.00.03
    % echo $USERNAME
    pf

The `cdpath` variable sets the search path for the `cd` command. If you do not specify `.` somewhere in the path, it is assumed to be the first component.

Example 16.4: cdpath directory search

    % cdpath=( /usr ~ ~/zsh )
    % ls /usr
    5bin         dict         lang         net          sccs         sys
    5include     etc          lector       nserve       services     tmp
    5lib         export       lib          oed          share        ucb
    adm          games        local        old          skel         ucbinclude
    bin          geac         lost+found   openwin      spool        ucblib
    boot         hosts        macsyma_417  pat          src          xpg2bin
    demo         include      man          princeton    stand        xpg2include
    diag         kvm          mdec         pub          swap         xpg2lib
    % cd spool
    /usr/spool
    % cd bin
    /usr/bin
    % cd func
    ~/func
    % cd
    % cd pub
    % pwd
    /u/pfalstad/pub
    % ls -d /usr/pub
    /usr/pub

`PATH` and `path` both set the search path for commands. These two variables are equivalent, except that one is a string and one is an array. If the user modifies `PATH`, the shell changes `path` as well, and vice versa.

Example 16.5: PATH and path linked

    % PATH=/bin:/usr/bin:/tmp:.
    % echo $path
    /bin /usr/bin /tmp .
    % path=( /usr/bin . /usr/local/bin /usr/ucb )
    % echo $PATH
    /usr/bin:.:/usr/local/bin:/usr/ucb

The same is true of `CDPATH` and `cdpath`:

Example 16.6: CDPATH and cdpath linked

    % echo $CDPATH
    /usr:/u/pfalstad:/u/pfalstad/zsh
    % CDPATH=/u/subbarao:/usr/src:/tmp
    % echo $cdpath
    /u/subbarao /usr/src /tmp

In general, predefined parameters with names in all lowercase are arrays; assignments to them take the form:

Example 16.7: Array assignment form

    name=( elem ... )

Predefined parameters with names in all uppercase are strings. If there is both an array and a string version of the same parameter, the string version is a colon-separated list, like `PATH`. You can use

Example 16.8: typeset tied parameters

    % typeset -T FOO foo

to create more of these yourself.

Example 16.9: Tied array and string

    % foo=( a b c )
    % echo $FOO
    a:b:c

`HISTFILE` is the name of the history file, where the history is saved when a shell exits.

Example 16.10: History file and size

    % zsh
    phoenix% HISTFILE=/tmp/history
    phoenix% SAVEHIST=20
    phoenix% echo foo
    foo
    phoenix% date
    Fri May 24 05:39:35 EDT 1991
    phoenix% uptime
      5:39am  up 4 days, 20:02,  40 users,  load average: 2.30, 2.20, 2.00
    phoenix% exit
    % cat /tmp/history
    HISTFILE=/tmp/history
    SAVEHIST=20
    echo foo
    date
    uptime
    exit
    % HISTSIZE=3
    % history
       28  rm /tmp/history
       29  HISTSIZE=3
       30  history

If you have several instances of `zsh` running at the same time, like when using the X window system, it might be preferable to append the history of each shell to a file when a shell exits instead of overwriting the old contents of the file. You can get this behaviour by setting the `APPEND_HISTORY` option.

In `zsh`, if you say

Example 16.11: Bare redirection alone

    % >file

the command `cat` is normally assumed:

Example 16.12: NULLCMD cat by default

    % >file
    foo!
    ^D
    % cat file
    foo!

Thus, you can view a file simply by typing:

Example 16.13: Bare input shows file

    % <file
    foo!

However, this is not csh or sh compatible. To correct this, change the value of the parameter `NULLCMD`, which is `cat` by default.

Example 16.14: NULLCMD as no-op

    % NULLCMD=:
    % >file
    % ls -l file
    -rw-r--r--  1 pfalstad        0 May 24 05:41 file

If `NULLCMD` is unset, the shell reports an error if no command is specified (like csh).

Example 16.15: Unset NULLCMD errors

    % unset NULLCMD
    % >file
    zsh: redirection with no command

Actually, `READNULLCMD` is used whenever you have a null command reading input from a single file. Thus, you can set `READNULLCMD` to `more` or `less` rather than `cat`. Also, if you set `NULLCMD` to `:` for sh compatibility, you can still read files with `< file` if you leave `READNULLCMD` set to `more`.