像 客 样 使 命令行 徐 东
1 1.1................................ 1 1.2................................. 3 1.3............................. 4 1.3.1 Linux............................ 5 1.3.2 macos............................ 6 1.3.3 Windows.......................... 6 1.4 Shell................................. 6 1.4.1 sh.............................. 7 1.4.2 csh.............................. 8 1.4.3 ksh.............................. 8 1.4.4 bash............................. 8 1.4.5 zsh.............................. 9 1.5............................. 9 1.5.1.......................... 10 1.5.2.......................... 11 1.5.3.......................... 11 1.6........................... 12 1.6.1................... 12 1.6.2................ 12 1.7............................ 13 15 2.1.............................. 15 2.2............................ 17 iii
iv 2.3........................ 17 2.4........................ 20 2.4.1 Zsh..................... 25 2.5................... 26 2.6............................. 31 2.6.1 bash.......................... 31 2.6.2 zsh........................... 33 37 3.1............................ 37 3.2............................ 39 3.3............................ 41 3.4......................... 41 3.5.................... 42 3.5.1........................ 42 3.5.2.......................... 42 3.5.3.......................... 43 3.6......................... 44 3.6.1.................... 44 3.6.2................ 44 3.6.3 n................ 45 3.7.................... 46 3.7.1..................... 46 3.7.2..................... 47 3.7.3........................ 47 3.7.4 n...................... 47 3.7.5 m n................... 48 3.7.6 n.................. 49 3.8..................... 49 3.8.1........................ 49 3.8.2........................ 50 3.8.3......................... 50 3.8.4................... 51 3.8.5................... 51 3.9....................... 52
v 53 4.1............................ 53 4.2 Emacs........................ 54 4.2.1....................... 54 4.2.2.................... 56 4.2.3....................... 58 4.2.4 Emacs.................... 60 4.3 vi.......................... 60 4.3.1.......................... 61 4.3.2.......................... 62 4.3.3.......................... 62 4.3.4.......................... 63 4.3.5.......................... 64 4.3.6.......................... 65 4.3.7 vi...................... 66 67 5.1.............................. 67 5.1.1....................... 67 5.1.2.................... 68 5.1.3........................ 69 5.1.4........................ 70 5.1.5.......................... 71 5.1.6......................... 71 5.2.............................. 73 5.2.1.......................... 74 5.2.2.......................... 75 5.2.3.......................... 75 5.2.4......................... 76 5.3 {}.......................... 76 5.3.1.......................... 77 5.3.2.......................... 77 5.3.3......................... 79 5.4.............................. 80 5.4.1.......................... 80
vi 5.4.2.......................... 81 5.4.3........................ 82 85 6.1.............................. 85 6.1.1 bash....................... 85 6.1.2 zsh........................ 95 6.2.............................. 104 6.2.1 z.lua.................... 105 111
2.1........... 30 3.1......................... 41 4.1 Emacs............. 56 4.2 Emacs........... 58 4.3 Emacs............. 59 4.4 vi.......................... 61 4.5 vi...................... 62 4.6 vi...................... 63 4.7 vi...................... 64 4.8 vi...................... 64 4.9 vi...................... 65 vii
viii
1.1 IBM 1620........................ 2 1.2 IBM 1620.................. 2 1.3 Linux....................... 3 1.4 DEC VT100.......................... 4 1.5 XTerm......................... 5 1.6 Shell............................. 7 1.7 zsh........................... 9 1.8............................. 13 2.1 bash...................... 17 2.2 GIMP.................... 20 2.3....................... 22 2.4.................... 23 2.5 zsh..................... 24 2.6 zsh....................... 26 2.7 bash................ 27 2.8 zsh................. 27 2.9....................... 29 2.10 bash................ 29 2.11 zsh................. 30 2.12 bash........................ 32 2.13 zsh........................ 34 3.1......................... 41 3.2 history 5......................... 45 3.3........................ 48 ix
x 3.4......................... 52 4.1 Emacs........................ 60 4.2 vi.......................... 66 6.1 Bash-it.......................... 87 6.2 Bash-it....................... 88 6.3 Bash-it....................... 89 6.4 Bash-it....................... 89 6.5 Bash-it git...................... 91 6.6 Bash-it......................... 94 6.7 Oh My Zsh........................ 96 6.8 Oh My Zsh........................ 97 6.9 man zsh..................... 99 6.10 sc-status sshd.................. 99 6.11 Oh My Zsh simple.................. 100 6.12 zsh-syntax-highlighting................. 102 6.13 zsh-syntax-highlighting.................. 103 6.14 zsh_stats.................... 104 6.15 Oh My Zsh.......................... 105
Bash Zsh xi
xii
https://selfhostedserver.com/usingcli-book https://selfhostedserver.com/ usingcli Version 2019.3.17 xiii
xiv
toy GNU/Linux DevOps https://linuxtoy.org 3000 Perl Git Twitter https://twitter.com/linuxtoy Mail xuxiaodong@pm.me 1 1 mailto:xuxiaodong@pm.me xv
xvi
Shell 1.1 Console System console Computer console Root console Operator s console 1.1 1 IBM 1620 1.2 Linux 1.3 Linux 2 1 https://en.wikipedia.org/wiki/system_console#/media/file:ibm_1620_model_1.jpg 2 https://en.wikipedia.org/wiki/linux_console#/media/file:knoppix-3.8-boot.png 1
2 1.1: IBM 1620 1.2: IBM 1620
1.2 3 1.3: Linux 1.2 Terminal 1.4 DEC VT100 3 Terminal emulator 3 https://en.wikipedia.org/wiki/computer_terminal#/media/file:dec_vt100_terminal.jpg
4 1.4: DEC VT100 1.3 1.5 Linux XTerm Linux macos Windows
1.3 5 1.5: XTerm 1.3.1 Linux XTerm 4 XTerm X DEC VT102 Tektronix 4014 ISO/ANSI GNOME Terminal 5 GNOME Terminal GNOME XTerm Konsole 6 Konsole KDE rxvt-unicode 7 rxvt-unicode rxvt unicode rxvt-unicode Daemon Perl 4 https://invisible-island.net/xterm/ 5 https://gitlab.gnome.org/gnome/gnome-terminal/ 6 https://kde.org/applications/system/konsole/ 7 http://software.schmorp.de/pkg/rxvt-unicode.html
6 1.3.2 macos Terminal.app Terminal.app macos TERM Man pages iterm2 8 iterm2 macos macos iterm2 1.3.3 Windows Mintty 9 Mintty Cygwin MSYS WSL XTerm 256 unicode Emoji ConEmu 10 ConEmu Windows 1.4 Shell Shell Shell Shell Unix Linux Kernel Shell 1.6 Unix Linux Shell sh csh ksh bash zsh 8 https://www.iterm2.com/ 9 https://mintty.github.io/ 10 https://conemu.github.io/
1.4 SHELL 7 1.6: Shell 1.4.1 sh sh Bourne shell Unix 7 Shell Bourne shell Stephen Bourne 1979 Unix Brian Kernighan Rob Pike sh Bourne shell Shell Linux sh Shell Debian 9 sh dash root@toydroid:~# ls -l /bin/sh lrwxrwxrwx 1 root root 4 Jan 24 2017 /bin/sh -> dash
8 Arch Linux sh bash root@codeland:~# ls -l /bin/sh lrwxrwxrwx 1 root root 4 Feb 7 15:15 /bin/sh -> bash 1.4.2 csh csh C shell Bill Joy BSD csh C csh Shell csh csh tcsh FreeBSD Shell 1.4.3 ksh ksh Korn Shell David Korn 1983 ksh POSIX Bourne shell C shell ksh vi Emacs ksh ksh pdksh public domain ksh ksh mksh Android Shell 1.4.4 bash bash GNU Bourne shell 1.4.1 Brian Fox bash 1989 bash Linux macos Shell WSL Windows Subsystem for Linux Windows 10 bash
1.5 9 bash Bourne-again shell POSIX sh csh ksh Shell 1.4.5 zsh zsh Z shell Paul Falstad 1990 zsh Bourne shell tcsh ksh bash Shell zsh 1.7 1.7: zsh bash zsh Shell 1.5 Command-line interface CLI Shell CLI GUI Graphical user interface
10 Linux macos Windows 1.5.1 xiaodong@codeland:~$ history awk '{CMD[$2]++;count++;}END \ { for (a in CMD)print CMD[a] " " \ CMD[a]/count*100 "% " a;}' grep -v "./" column -c3 -s " " -t sort -nr nl head -n10 macos 1 1348 14.3771% cd 2 1034 11.0282% l 3 838 8.93771% git 4 569 6.06869% ssh 5 513 5.47142% cat 6 405 4.31954% vim 7 372 3.96758% brew 8 360 3.83959% scp 9 265 2.82637% rm 10 264 2.8157% grep
1.5 11 history awk grep column sort nl head 7 10 1.5.2 photos mar_photos.txt Linux GNOME Files macos Finder photos xiaodong@codeland:~$ cd photos; \ ls -l grep 'Mar' awk '{ print $9 }' > mar_photos.txt 1.5.3 Shell Function Script xiaodong@codeland:~$./script.sh
12 1.6 Shell Shell 1.6.1 Linux Linux login: Password: Linux 1. Ctrl + Alt + F1 1 2. Ctrl + Alt + F2 2 3. 3 4 5 6 Linux 6 4. Ctrl + Alt + F7 tty 1.6.2 1.3 Linux rxvt-unicode macos iterm2
1.7 13 Shell Shell Shell Shell Shell chsh Shell Shell zsh xiaodong@codeland:~$ chsh -s /bin/zsh Shell echo $SHELL 1.7 C Brian W. Kernighan Dennis M. Ritchie Hello world 1.8 1.8: 1.8 1. xiaodong 2. codeland hostname -s 3. ~ Linux /home/< > macos /Users/< > 4. $ root bash root #
14 5. echo -e "\t " echo -e \t " ' " 5 @ : ` ` @ : $ # echo -e "\t " BackSpace Delete Enter echo \t Tab xiaodong@codeland:~$ echo -e "\t "