Vous êtes sur la page 1sur 29

Conquering the

Command Line
With ZSH
Fabian Kislat
DESY IceTray Seminar
Dec 01, 2009

What is ZSH?

Dec 01, 2009

A Unix shell

bash like syntax

Standard Login shell for users in Zeuthen

Very customizable

Will focus on using zsh

Not shell programming in general


Fabian Kislat - Conquering the Command Line With ZSH

Today's Topics

Dec 01, 2009

Configuring the shell

Functions, scripts and autoload

Useful keyboard shortcuts

History

Globbing

Parameter Expansion

A little shell scripting


Fabian Kislat - Conquering the Command Line With ZSH

Part I:
Configuring the Shell

The startup files


Three startup files: .zshenv, .zprofile, .zshrc
All shells
On the batch farm
.zshenv
When running scripts
Login shell?

.zprofile

Interactive shell?

.zshrc

Logout file: .zlogout

Important: No output in .zshenv!

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

In an xterm

Keybindings

Default keybindings mode: emacs


Ctrl+K, Ctrl+Y, Alt+d,

Dec 01, 2009

Other useful keys:


Alt+q Interrupt current command
Good for a 'man' while
typing a long line
Ctrl+Z Suspend current job,
continue in background
with bg
Fabian Kislat - Conquering the Command Line With ZSH

Defining Keybindings

Use bindkey to create custom keybindings


Example:
bindkey '^[[3~' delete-char

Command names as in emacs

Use read to find the in-string

Change to vi keymap: bindkey -v

Dec 01, 2009

You can use bindkey to create and manage


your own keymaps
Fabian Kislat - Conquering the Command Line With ZSH

Alias and Function

Don't type more than you need to: alias

Example: alias ll=ls -lh

Dec 01, 2009

Shell is a programming language


Supports functions
function foo {
# commands
}

foo () {
# commands
}

Very much like a shell script


Fabian Kislat - Conquering the Command Line With ZSH

Functions Vs. Scripts

Function:

Parsed when defined


Many functions in startup file slow down
shell startup

Script:

Dec 01, 2009

Runs in current shell


Can modify current context

Runs in sub-shell
Cannot influence current shell
Full shell startup processed when run
Does not affect startup of your shell
Fabian Kislat - Conquering the Command Line With ZSH

Autoload

Can we have the best of both worlds?

Enter: Autoload

autoload foo
Now foo is loaded on demand,
i.e. when run the first time
Runs in current shell

No shell startup initialization

Can modify current context

Parsed when needed

Dec 01, 2009

Small influence on startup time


Fabian Kislat - Conquering the Command Line With ZSH

10

Autoload

How to do that?
Write a script (e.g. called foo),
put it into some directory (often ~/.zfunc)
Add directory to the FPATH list:
FPATH=~/.zfunc:$FPATH

Autoload it: autoload foo

Now you can use it

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

11

Options

You can customize ZSH using setopt and


unsetopt
Example: unsetopt sh_word_split
Options are case insensitive and _ is ignored:
shwordsplit, ShWordSplit,
S_h_W_o_R_d_S_p_L_i_T
are equivalent

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

12

Modules

Allow extending the zsh features

Are written in C (binary modules!)

Load them with zmodload

Dec 01, 2009

Useful in scripts:
zmodload zsh/mathfunc
Makes mathematical functions available
I would load them only when needed
Fabian Kislat - Conquering the Command Line With ZSH

13

Sample Startup Files

http://www-zeuthen.desy.de/~kislat/zstartup.tar.gz

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

14

Part II:
Interactive Features

Shell History

Type or to scroll through history

Use Ctrl+R to search your history

Dec 01, 2009

Set the variables HISTSIZE and


SAVEHIST to control how many lines
or history are stored
Variable HISTFILE controls where the
history is stored

Fabian Kislat - Conquering the Command Line With ZSH

16

More History

Control how commands are stored:

setopt hist_ignore_dups
Ignore duplicate lines
setopt hist_ignore_space
Ignore lines starting with whitespace when saving
Many more options

Access history:

!! entire last command

!$ last command argument

Useful: bindkey ' ' magic-space

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

17

Modifiers

You can modify the result you get from


the shell history:

:h Head, only the directory

:t Tail, only the file name

:r Path without extension

Example:

$ echo foo/bar.txt
foo/bar.txt
$ echo !$:t
bar.txt

Also used in globbing and parameter expansion

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

18

Globbing

Eh? What's that?

Automatic generation of file lists, e.g. *

? matches a single character

* matches any number of characters (including .)


including no characters
** matches any number of directories including
the current directory

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

19

Globbing

Match groups: [abc], [[:alpha:]],


(Think of regular expressions)
Match numbers:
<10-15> matches 10, 11, 12, 13, 14, 15
<1-10> matches 1, 2, 3, but also 01, 02, ...
You can use Modifiers.
Example: echo */*.root(:t)
(:h) and (:t) replace dirname and basename

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

20

Globbing

Dec 01, 2009

Group patterns (back referencing!):


(pat) matches pat
(pat1|pat2) matches pat1 or pat2
Glob qualifiers:

(/) match directories

(.) match plain files

(D) also match hidden files

Extend the possibilities with


setopt extended_glob
Fabian Kislat - Conquering the Command Line With ZSH

21

Part III:
Scripting

Parameter Expansion

Use it: echo ${var} or echo $var

You can use modifiers: ${file:t}

Dec 01, 2009

Define a variable: var=value


No spaces around =

Remove pattern from


head:
${var#pat}
tail:
${var%pat}
You can nest this:
${${${var%.root}#*Run}:r}
${#var}

length of var

Fabian Kislat - Conquering the Command Line With ZSH

23

Parameter Expansion II

Some expansions modify the variable

Dec 01, 2009

e.g. ${var:=str}
$var if set else str and set var to it

Useful to set default values:


${var:=default}
would produce the command line:
default
error, command not found
Prepend a colon to suppress execution
: ${var:=default}
Fabian Kislat - Conquering the Command Line With ZSH

24

Using the output of a command

Dec 01, 2009

Get the output of command:


`command` or $(command)
Get current directory:
current_dir=`pwd`
Get directory where script is located:
mydir=$(cd `dirname $0` && pwd)
Read a file as an array (one item per line):
data=(${(f)$(<file)})
Fabian Kislat - Conquering the Command Line With ZSH

25

Shell Variables

$# number of parameters (script, function)

$* all parameters

$0 name of script, function

$? return value of last command

Basename of this script: ${0:t}

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

26

Arrays

Create an array: array=(a b c d)

Use it: echo ${array[1]} a

Dec 01, 2009

Split variable to array on new line:


array=(${(f)var})
Now you can read a file:
cont=(${(f)$(<file.txt)})
Parameter modifiers work per element:
newarray=${oldarray%.gz}
Fabian Kislat - Conquering the Command Line With ZSH

27

Miscellaneous

One more thing on arrays:


Link them with a scalar, like PATH,
LD_LIBRARY_PATH:
declare -T LD_LIBRARY_PATH \
ld_lib_path
If either is modified, the other changes as well
Local variables (useful in functions):
local x=5

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

28

Useful References

ZSH reference card:


http://www.bash2zsh.com/zsh_refcard/refcard.pdf

ZSH texinfo manual:


http://zsh.sourceforge.net/Doc/zsh_a4.pdf

The book
O. Kiddle, J. Peek, and P. Stephenson, From
Bash to Z Shell Conquering the Command Line,
APress

Dec 01, 2009

Fabian Kislat - Conquering the Command Line With ZSH

29

Vous aimerez peut-être aussi