About Zsh

Zsh environment settings

Using UNIX

Zsh environment settings


    For zsh environment settings, it is necessary to describe the following items (or something similar) in the main setting file. The setting of each user's unique zsh is stored on the '~/.zshrc' file. ('rc' is derived from the executed file /etc/*rc family at UNIX startup, and is an abbreviation of 'run' command)

    1. ·Settings for limit, umas, stty etc
    2. ·Settings for environment variables 'PATH', 'LD_LIBRARY_PATH' etc
    3. ·Settings for shell variables 'PROMPT','SAVEHIST' etc
    4. ·Settings for alias
    5. ·Settings for function
    6. ·Settings for autoload (add function)
    7. ·Settings for bindkey (key bindings)
    8. ·Settings for zstyle (style such as completion)
    9. ·Settings for zshoptions (option)
    These settings will be changed for each OS by the `OSTYPE` shell variable, as required. The export command declares variables as environment variables. The 'alias' command sets another name for the command. By using the function command, a difficult command is set as the function. For users familiar with key bindings such as Emacs or vi editor, it is possible to use a multiline editor and the advanced command-line editing function of zsh by setting either key bindings by 'bindkey-e' or '-v'. The precmd command is a special function executed every time before the prompt is displayed. 'zstyle' and 'setopt' can be used to radically change the behavior of zsh, and provides many options.

    % man zshoptions
    % man zshall

    Refer to the information above; please set the system in such a way that it is easy to use.

    Click here to download a sample file.


    This file is executed only once when exiting the shell. For example, the screen will be cleared at logout if 'clear' in written in this file.


    This file maintains a history of employed commands. The number of lines of commands specified on 'SAVEHIST' shell variables will be kept as a record. The command history can be searched using the '↑' arrow or 'Ctrl-R' for the next zsh run, which avoids the need to re-enter long command lines.

Tips for Zsh

    Zsh possesses the following convenient functions that are not found in other shells:

    1. ·Function for selecting a contextual menu for the completion candidate
    2. ·History retrieval function using 'Ctrl-r' and 'Ctrl-s'
    3. ·Embedded multiline editor (zle)
    4. ·Embedded environment-variable editor (vared)
    5. ·Recursive and comprehensible file match **/*(@)
    6. ·Various command-line editing functions (e.g., kill-region)
    7. ·Command full path expansion function (=blastall)
    Some of the tips of zsh are shown.

Selection by file attribute

    Modify the wild-card term with (), and

    % ls /etc/**/*#All files under /etc
    % ls /etc/**/*(.)#Normal files under /etc
    % ls /etc/**/*(@)#Link under /etc
    % ls -d /etc/**/*(/)#Directory under /etc
    select a file with a specific attribute (as shown).

    % ls -l *(L0)#Files with size of 0 only
    % ls -l *(f700)#rwx------files only
    % ls -l *(u[hoge])#Files of user hoge only
    % ls -l *(g[db])#Files of group db only
    In addition, it is possible to select by file mode

    % ls -l *(mw-3)#Files updated within the last 3 weeks
    % ls -l *(m0)#Files updated today
    % ls -l *(mM+6)#Files at least 6 months old
    % ls -l *(ch-2)#Files moved within the last 2 hours
    and timestamp.

    If specifying the linked file, not the link file itself, place '-' at the beginning. Use '(-/)' for a directory link.

    % ls -l *(-/)
    % ls -l *(-m0)

    Detailed method for specifying the timestamp

    1. Specifying the type of time
    There are three types of file timestamp:
      m:time last modified, a:time last accessed, c:time when the 'i-node' was last changed

    2. Specifying the unit of time
    If nothing is specified, the default setting is days.
      M:months W:weeks h:hours m:minutes s:seconds

    3. Specifying the timespan
    If nothing is specified, the timespan will be exactly the specified time (e.g., files modified exactly n days ago, files modified exactly m minutes ago).
       +:files modified before the specified time  
       -:files modified after the specified time

    4. Specifying the numerical value
    Specify the numerical value that indicates the amount of time.

Files from m to n

    It is possible to specify the n-th file of files that matches the file pattern.

    For example, in the case that the file name starts with 'DSC' and the user wishes to match the second to fifth files:

    % rm DSC*([2,5])

    When written as below, the file name starts with 'DSC', and the last character is 2 or 5.

    % rm DSC*[2,5]

Extract the number list

    This is used to produce the order of numbers with {m..n}, which is useful when processing files in sequential order or part of a file.

    % mv DSC0{3769..3822}/jpg photo/
    % for i in {1..10}
      cp foo$1.txt bar$1.txt

File "other than this pattern"

    By adding the option of 'setopt extended_glob' to the '.zshrc.all' file and using '~', it is possible to specify all files except for "just this pattern".

    % ls *~CVS#All files except CVS
    % rm *.o~hoge.o#All .o files except hoge.o
    % ls *~*[0-9]*#All files except files with numbers

Removing the path name from the file name

    A similar operation, using the 'basename' or 'dirname' command for the file name, can only be performed using the zsh function.

    #Assign path of the 'ruby' command(=ruby)
    % file==ruby
    #Corresponds to the 'basename' command
    % echo $file#/usr/local/bin/ruby
    #Corresponds to the 'basename' command
    % echo $file:t#ruby
    #Corresponds to the 'dirname' command
    % echo $file:h#/usr/local/bin

Handling standard error

    It is possible to use both standard output (1) and standard error (2) in the sh shell and bash shell (B shell family). To combine, use 2>&1.

    % command 1> out.log 2> err.log#To a different file
    % command 2>&1 | less#Combine and to a pipe
    % command > outerr.log 2>&1#Combine and to a file

    It is possible to use both standard output (1) and standard error (2) in the csh shell and tcsh shell (C shell family). To combine, use 2>&1.

    % (command1 > out.log) >& err.log#To a different file
    % command |& less#Combine and to a pipe
    % command >& outerr.log#Combine and to a file

    For zsh, it is possible to use both separate syntax with 1> and 2> of the B shell family, and combined syntax of |& and >&.

Input-output of multiple commands

    When using the 'paste' command, the 'cut' command, and the <(command) syntax of zsh, it is possible to perform an operation such as "combine the first column of file 1 and the third column of file 2" without using an intermediate file.

    % paste <(cut -f1 file1) <(cut -f3 file2)

    Moreover, you can send the result to multiple processes by using >(command) syntax. This makes it possible to process output more flexibly than the combination of '|' and 'tee' (it is necessary to set up 'setopt multios' when using this function).

    % paste <(cut -f1 file1) <(cut -f3 file2)
       > >(ruby -e 'p')
       > >(perl -e 's/~/perl:/')
    % date > >(tac) >(cat)

Waiting for completion of the sub-process

    When using the 'wait' syntax, you can wait for completion of the sub-process that was executed in parallel by the shell script.

    #Prepare FASTA format file
    #Execute BLAST
    for s in *.seq
      blastall -p blastp -i $s -d db > $s.out &
    #Analyze the result

Top of Page Top of Page

The University of Tokyo The Institute of Medical Science

Copyright©2005-2019 Human Genome Center