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