z shell – the last shell you’ll ever need
DESCRIPTION
Z Shell – the last shell you’ll ever need. changlp. Introduction - Shell. 殼 Fetch → Analyze → Execute Fork Type. Introduction – ZSH (1/2). Originally by Paul Falstad in 1990 A shell designed for interactive use Powerful user experience Use sh as underlying syntax - PowerPoint PPT PresentationTRANSCRIPT
Z ShellZ Shell – the last shell you’ll ever need – the last shell you’ll ever need
changlp
Com
pu
ter C
en
ter, C
S, N
CTU
2
Introduction - ShellIntroduction - Shell
殼 Fetch → Analyze → Execute
• Fork
Type
Login Non-login
Interactive User’s shell Shell executed after login
Non-interactive
? Script
Com
pu
ter C
en
ter, C
S, N
CTU
3
Introduction – ZSH (1/2)Introduction – ZSH (1/2)
Originally by Paul Falstad in 1990 A shell designed for interactive use
• Powerful user experience
Use sh as underlying syntax• Easy for using / migrating
Completion & correction Available in ports: shells/zsh
Com
pu
ter C
en
ter, C
S, N
CTU
4
Introduction – ZSH (2/2)Introduction – ZSH (2/2)
% rsync -<TAB>
% cd /usr/lica<TAB>
Com
pu
ter C
en
ter, C
S, N
CTU
5
Startup Files (1/2)Startup Files (1/2)
For all ZSH• /etc/zshenv
• ~/.zshenv
If [ login shell ]• /etc/zprofile
• ~/.zprofile
If [ interactive ]• /etc/zshrc
• ~/.zshrc
If [ login shell ]• /etc/(z)login
• ~/.(z)login
Login Non-login
Interactive User’s shell Shell executed after login
Non-interactive
? Script
Com
pu
ter C
en
ter, C
S, N
CTU
6
Startup Files (2/2)Startup Files (2/2)
zshenv• Settings for ALL types of ZSH
zprofile• Settings for each login session (path … etc)
zshrc• Some interactive settings (prompt … etc)
zlogin (zlogout)• Execute some program on login / logout
Com
pu
ter C
en
ter, C
S, N
CTU
7
IO Redirection - MultiosIO Redirection - Multios
% date >file1 >file2 … >fileN % date >file1 | prog1 % cat -n < /var/log/**/*log(R.)
• All files under /var/log and its subdir with 0004 permission
% cat bar | sort <foo• % cat bar foo | sort # note the order
Com
pu
ter C
en
ter, C
S, N
CTU
8
IO Redirection - Null RedirectionIO Redirection - Null Redirection
% < file• more or $READNULLCMD
% > file• cat or $NULLCMD
Com
pu
ter C
en
ter, C
S, N
CTU
9
Filename Gerneration – Extended GlobbingFilename Gerneration – Extended Globbing
Common shells support simple globbing• ls –lh *
• ls –lh *.[co]
• ls –lh *.[^oh]
ZSH supports extended globbing• ls –lh ^*.c
• ls –lh ^Makefile
• Option: EXTENDEDGLOB (default on)
Com
pu
ter C
en
ter, C
S, N
CTU
10
Filename Generation – GlobbingFilename Generation – Globbing
Recursive globbing **/
All dirs and subdirs
***/ Also follow symbolic links
• Ex. $ ls –lh **/foo Searches foo under all dir & subdir
Form: (flags)pattern,operator …(qualifier)• Ex. $ ls –lh (#i)[[:digit:]]ggyy.[^oh](R.)
Com
pu
ter C
en
ter, C
S, N
CTU
11
Filename Generation – Globbing flagsFilename Generation – Globbing flags
Globbing flags Affect to their right
Until end of enclosing group or end of the pattern
May interleave with operators $ ls -lh (#i)Make(#I)file
• #I/i: case sensitivity
• #cM,N: {M,N} in regex
• #a: Approximate
• …
$ ls1ggyy.c MakeFile Makefile Mekefile Mikefile Mokefile Mukefile Nukefile makefile$ ls (#i)Make(#I)fileMakefile makefile
Com
pu
ter C
en
ter, C
S, N
CTU
12
Filename Generation – Globbing operatorFilename Generation – Globbing operator
Globbing operator Cooperate with patterns Like regex
• * This is self explanatory
• [] Match enclosed characters Named class
– [:alpha:], [:digit:]…
Use “^” or “!” for negation
• () Grouping
• …
Com
pu
ter C
en
ter, C
S, N
CTU
13
Filename Generation – Globbing qualifierFilename Generation – Globbing qualifier
Globbing qualifier Specify which filenames should be argument list
• /: directories
• .: plain files
• @: links
• +cmd: filename will be included if cmd returns a zero status
• …
Com
pu
ter C
en
ter, C
S, N
CTU
14
Filename Generation – ReferenceFilename Generation – Reference
For further glob information• http://
zsh.sourceforge.net/Doc/Release/Expansion.html#Filename-Generatio
• Have a look on other types of expansion
ZSH 愛用者
find(1) 是什麼小朋友 ?可以吃嗎 ?Open!
Com
pu
ter C
en
ter, C
S, N
CTU
15
ScriptScript
Bourne shell based Globbing Expansion
• zshexpn(1)
Portability• Use zsh built-ins if possible
• Module support
Com
pu
ter C
en
ter, C
S, N
CTU
16
Script – Type SystemScript – Type System
Data Types• Array
Slice
• Scalar (string)
• Float
• Integer
• Association (hash) % typeset -A blah
% typeset -r agent• Constant
% typeset -U unique_set• Unique set
…
Com
pu
ter C
en
ter, C
S, N
CTU
17
Script - ModuleScript - Module
zsh/curses zsh/mathfunc zsh/net/tcp zsh/example …
Example• http://zshwiki.org/home/code/scripts/zshttpd
Com
pu
ter C
en
ter, C
S, N
CTU
18
CompletionCompletion
<TAB>, and shell tries to fill the rest In (t)csh
• More in: /usr/share/examples/tcsh/complete.tcsh
In bash
• More in: shells/bash-completion
Completion distributed separately
complete sudo 'p/1/c/'complete chown 'p/1/u/‘complete cd ‘p/1/d/’
complete -f -X '!*.@(Z|[gGd]z|t[ag]z)' gunzip zcat unpigz
Com
pu
ter C
en
ter, C
S, N
CTU
19
Completion in ZSHCompletion in ZSH
Programmable completion Context sensitive
• % echo $OS<TAB>
• % cd <TAB>
• % ssh <TAB>
Completion function distributed with ZSH• [FreeBSD] See
/usr/local/share/zsh/<version>/functions/Completion/
Com
pu
ter C
en
ter, C
S, N
CTU
20
Completion Context (1/2)Completion Context (1/2)
:completion:<func>:<completer>:<command>:<argument>:<tag>• Current context while completing
• How completion arrived here so far
Func• Blank if called from standard completion system
Completer• Completer name (‘completer’ in standard completion case)
• Correction… etc
Command Argument Tag
• Things can be completed from this point
Com
pu
ter C
en
ter, C
S, N
CTU
21
Completion Context (2/2)Completion Context (2/2)
% cd• :completion::complete:cd::
% git add• :completion::complete:git-add:: (+ more arguments)
• :completion::complete:git:: (+ more subcommands)
Tag: modified-files other-files ignored-modified-files ignored-other-files untracked-files
Com
pu
ter C
en
ter, C
S, N
CTU
22
Completion StylesCompletion Styles
Control the behavior of completers zstyle ':completion:*' menu select zstyle ':completion:*:kill:*' force-list always zstyle ':completion:*' completer _complete _correct
_approximate
Com
pu
ter C
en
ter, C
S, N
CTU
23
Summing UpSumming Up
Pros• Powerful completion system
Good for interactive Built-in completion support for most UNIX programs
Cons• Bad for
• Slow Completion system overhead
• Complex configuration
Com
pu
ter C
en
ter, C
S, N
CTU
24
Live DemoLive Demo
如果有東西壞了請當作沒有看到
Com
pu
ter C
en
ter, C
S, N
CTU
25
Some Plugins & ResourseSome Plugins & Resourse
https://github.com/zsh-users/zsh-syntax-highlighting https://github.com/robbyrussell/oh-my-zsh From Bash to Z Shell: Conquering the Command Line ZSH manual