1 | fishshell
|
2 | ===
|
3 |
|
4 | 比 bash 更好用的 shell
|
5 |
|
6 | ## 安装
|
7 |
|
8 | ```shell
|
9 | # Ubuntu 和 Debian 的安装方法。
|
10 | sudo apt-get install fish
|
11 | # Mac 的安装方法。
|
12 | brew install fish
|
13 | ```
|
14 |
|
15 | ## 启动与帮助
|
16 |
|
17 | 由于 `Fish` 的语法与 `Bash` 有很大差异,`Bash` 脚本一般不兼容。因此,建议不要将 `Fish` 设为默认 `Shell`,而是每次手动启动它。
|
18 |
|
19 | ```shell
|
20 | # 安装完成后,就可以启动 Fish。
|
21 | $ fish
|
22 | # 使用过程中,如果需要帮助,可以输入 help 命令
|
23 | $ help
|
24 | ```
|
25 |
|
26 | ## 彩色显示
|
27 |
|
28 | ```shell
|
29 | # 无效命令为红色
|
30 | $ mkd
|
31 | # 有效命令为蓝色
|
32 | $ mkdir
|
33 | # 有效路径会有下划线。如果没有下划线,你就知道这个路径不存在。
|
34 | $ cat ~/somefi
|
35 | ```
|
36 |
|
37 | ## 自动建议
|
38 |
|
39 | Fish 会自动在光标后面给出建议,表示可能的选项,颜色为灰色。如果采纳建议,可以按下 `→` 或 `Control + F` 。如果只采纳一部分,可以按下 `Alt + →`。
|
40 |
|
41 | ```shell
|
42 | $ /bin/hostname # 命令建议
|
43 | $ grep --ignore-case # 参数建议
|
44 | $ ls node_modules # 路径建议
|
45 | ```
|
46 |
|
47 | ## 自动补全
|
48 |
|
49 | 输入命令时,`Fish` 会自动显示匹配的上一条历史记录。如果没有匹配的历史记录,`Fish` 会猜测可能的结果,自动补全各种输入。比如,输入 `pyt` 再按下 `Tab` ,就会自动补全为 `python` 命令。
|
50 |
|
51 | `Fish` 还可以自动补全 `Git` 分支。
|
52 |
|
53 | ## 脚本语法
|
54 |
|
55 | ### if 语句
|
56 |
|
57 | ```shell
|
58 | if grep fish /etc/shells
|
59 | echo Found fish
|
60 | else if grep bash /etc/shells
|
61 | echo Found bash
|
62 | else
|
63 | echo Got nothing
|
64 | end
|
65 | ```
|
66 |
|
67 | ### switch 语句
|
68 |
|
69 | ```shell
|
70 | switch (uname)
|
71 | case Linux
|
72 | echo Hi Tux!
|
73 | case Darwin
|
74 | echo Hi Hexley!
|
75 | case FreeBSD NetBSD DragonFly
|
76 | echo Hi Beastie!
|
77 | case '*'
|
78 | echo Hi, stranger!
|
79 | end
|
80 | ```
|
81 |
|
82 | ### while 循环
|
83 |
|
84 | ```shell
|
85 | while true
|
86 | echo "Loop forever"
|
87 | end
|
88 | ```
|
89 |
|
90 | ### for 循环
|
91 |
|
92 | ```shell
|
93 | for file in *.txt
|
94 | cp $file $file.bak
|
95 | end
|
96 | ```
|
97 |
|
98 | ### 函数
|
99 |
|
100 | `Fish` 的函数用来封装命令,或者为现有的命令起别名。
|
101 |
|
102 | ```shell
|
103 | function ll
|
104 | ls -lhG $argv
|
105 | end
|
106 | ```
|
107 |
|
108 | 上面代码定义了一个 `ll` 函数。命令行执行这个函数以后,就可以用 `ll` 命令替代 `ls -lhG`。其中,变量 `$argv` 表示函数的参数。
|
109 |
|
110 | ```shell
|
111 | function ls
|
112 | command ls -hG $argv
|
113 | end
|
114 | ```
|
115 |
|
116 | 上面的代码重新定义 `ls` 命令。注意,函数体内的 `ls` 之前,要加上 `command`,否则会因为无限循环而报错。
|
117 |
|
118 | ### 提示符
|
119 |
|
120 | `fish_prompt` 函数用于定义命令行提示符(prompt)。
|
121 |
|
122 | ```shell
|
123 | function fish_prompt
|
124 | set_color purple
|
125 | date "+%m/%d/%y"
|
126 | set_color FF0
|
127 | echo (pwd) '>'
|
128 | set_color normal
|
129 | end
|
130 | ```
|
131 |
|
132 | 执行上面的函数以后,你的命令行提示符就会变成下面这样。
|
133 |
|
134 | ```
|
135 | 02/06/13
|
136 | /home/tutorial >
|
137 | ```
|
138 |
|
139 | ## 配置
|
140 |
|
141 | Fish 的配置文件是 `~/.config/fish/config.fish`,每次 `Fish` 启动,就会自动加载这个文件。Fish 还提供 Web 界面配置该文件。
|
142 |
|
143 | ```shell
|
144 | $ fish_config # 浏览器打开 Web 界面配置
|
145 | ```
|
146 |
|
147 | Running Commands: 兼容 bash 等shell的命令执行方式
|
148 | Getting Help: `help/man cmd -> browser/terminal`
|
149 | Syntax Highlighting: 实时检查命令是否正确
|
150 | Wildcards: 支持缩写 `*` 递归 匹配
|
151 | Pipes and Redirections: 使用 `^` 代表 stderr
|
152 | Autosuggestions: 自动建议, 可以使用 `Ctrl-f / ->` 来补全
|
153 | Tab Completions: 更强大的 tab 补全
|
154 | Variables: 使用 set 设置
|
155 | Exit Status: 使用 `echo $status` 替代 `$?`
|
156 | Exports (Shell Variables)
|
157 | Lists: all variables in fish are really lists
|
158 | Command Substitutions: 使用 `(cmd)` 来执行命令, 而不是 反引号、`$()`
|
159 | Combiners (And, Or, Not): 不支持使用符合来表示逻辑运算
|
160 | Functions:使用 `$argv` 替代 `$1`
|
161 | Conditionals (If, Else, Switch) / Functions / Loops: 更人性化的写法(参考 py)
|
162 | Prompt: `function fish_prompt` 实现
|
163 | Startup (Where's .bashrc?): `~/.config/fish/config.fish`,更好的方式是 autoloading-function、universal-variables
|
164 | Autoloading Functions: ` ~/.config/fish/functions/.`
|
165 | Universal Variables:a variable whose value is shared across all instances of fish
|
166 |
|
167 | ```shell
|
168 | set name 'czl' # 设置变量,替代 name=czl
|
169 | echo $name
|
170 | echo $status # exit status,替代 $?
|
171 | env # 环境变量
|
172 | set -x MyVariable SomeValue # 替代 export
|
173 | set -e MyVariable
|
174 | set PATH $PATH /usr/local/bin # 使用 lists 记录 PATH
|
175 | set -U fish_user_paths /usr/local/bin $fish_user_paths # 永久生效
|
176 | touch "testing_"(date +%s)".txt" # command subtitution,替代 `date +%s`
|
177 | cp file.txt file.txt.bak; and echo 'back success'; or echo 'back fail' # combiner
|
178 | functions # 列出 fish 下定义的函数
|
179 | ```
|
180 |
|
181 | ## 参考资料
|
182 |
|
183 | - [fish-shell官网](http://fishshell.com) |
\ | No newline at end of file |