本文根据 Devhints / Fish shell 翻译。代码示例及版本范围沿用原文。原文标注更新日期:2018-01-31。
按键
| 快捷键 | 说明 |
|---|---|
^A ←/^E → | 移动到行首/行尾 |
Alt ←/Alt → | 跳转到上一个/下一个单词 |
↑/↓ | 切换到上一个/下一个命令 |
Alt ↑/Alt ↓ | 切换到上一个/下一个参数 |
^U | 删除至行首 |
^C | 取消当前行 |
Alt H | 显示命令的帮助手册 (man page) 说明 |
Alt W | 显示命令的简要说明 |
Alt . | 重复上一个参数 |
示例程序
#!/usr/bin/env fish
echo 'Hello from Fish!'
注释
# my comment
打印
echo 'Hello from Fish!'
# or
printf '%s\n' 'Hello from Fish!'
打印字符串(末尾附带内容) \n.
从标准输入(stdin)读取
read my_variable
将字符串读入变量 my_variable.
循环
for i in (seq 1 10)
...
end
变量
定义与清除
# Declare the global/local variable:
set my_variable 'Hello from Fish!'
i# Remove the variable:
set --erase my_variable
切片
echo $my_variable[1..10]
echo $my_variable[2..]
echo $my_variable[..-2]
数字
递增与递减
set my_variable (math $my_variable + 1)
set my_variable (math $my_variable - 1)
算术运算
echo (math 1 + 2)
| 运算符 | 执行 |
|---|---|
+ | 加法 |
- | 减法 |
* | 乘法 |
/ | 除法 |
% | 取模 |
^ | 幂运算 |
字符串
匹配
将字符串与正则表达式进行匹配:
string match --regex --entire 'Fish' 'Hello from Fish!'
| 模式 | 匹配项 |
|---|---|
x? | 零个或一个 x 字符 |
x* | 任意数量 x 字符 |
x+ | 一个或多个 x 字符 |
x{n} | n 次 x 字符 |
x{n,m} | n 到 m 次 x 字符 |
x{n,} | n 次或更多次 x 字符 |
[xy] | x 或 y 字符 |
[^xy] | 非 x 或 y 字符 |
\w | 单词字符 |
\d | 数字字符 |
\W | 非单词字符 |
\D | 非数字字符 |
此处介绍的是与 Perl 兼容的正则表达式。
替换
# Replaces the first match
string replace --regex 'Fish' 'fish' 'Hello from Fish!'
# Replaces all matches
string replace --regex --all 'Fish' 'fish' 'Hello from Fish!'
条件判断
If/else
if test $my_variable -lt $another_variable
···
else if test $my_variable -eq $another_variable
···
else
···
end
比较
数字
| 数值运算符 | 含义 |
|---|---|
-lt | 小于 |
-eq | 等于 |
-gt | 大于 |
-le | 小于或等于 |
-ge | 大于或等于 |
-ne | 不等于 |
字符串
| 字符串运算符 | 含义 |
|---|---|
= | 等于 |
!= | 不等于 |
文件
| 文件运算符 | 含义 |
|---|---|
-f | 文件存在 |
-d | 目录存在 |
-r | 文件或目录存在且可读 |
-w | 文件或目录存在且可写 |
-x | 文件或目录存在且可执行 |
进程通信
写入文件
# Overwrite file
echo 'Hello from Fish!' > my_file
# Append to file
echo 'Hello from Fish!' >> my_file
管道 (Piping)
my_command | another_command
将第一个命令的标准输出(stdout)作为输入传递给第二个命令。
命令替换
echo (math $my_variable + 1)
该 (...) 表达式被替换为其内部命令的输出结果。
进程替换
echo (math $my_variable + 1 | psub)
该 (... | psub) 表达式被替换为一个包含命令输出结果的临时文件。
函数
定义与清除
# Declare the function
function my_function --description 'My description'
···
end
# Remove the function
functions --erase my_function
事件
触发(发送)
emit my_event
触发一个可被其他函数捕获的事件。
事件处理
function my_hook --on-event my_event
···
end
响应 my_event 事件。
缩写
定义与清除
# Declare the abbreviation
abbr --add grh "git reset --hard HEAD"
# Remove the abbreviation
abbr --erase grh
补全
定义补全
complete --command mycommand --arguments 'install uninstall'
complete --command mycommand --short-option 'h' --long-option 'help' --description 'Display help'
| 选项 | 说明 |
|---|---|
--arguments | 命令本身或选项的参数 |
--short-option | 短选项 |
--long-option | 长选项 |
--no-files | 不建议补全文件 |
--force-files | 建议补全文件 |
--condition | 仅在满足特定条件时显示提示 |
--description | 说明 |
声明命令的补全规则。
移除补全规则
complete --command mycommand --erase
实用的内置函数
| 函数 | 说明 |
|---|---|
__fish_seen_argument | 检查是否使用了指定的参数 |
__fish_seen_subcommand_from | 检查是否使用了指定的子命令 |
__fish_use_subcommand | 检查是否使用了任何子命令 |
__fish_complete_directories | 补全名称中包含指定字母的目录 |
__fish_complete_suffix | 补全具有指定后缀的文件 |
__fish_complete_users | 列出所有用户 |
__fish_complete_groups | 列出所有用户组 |
__fish_print_hostnames | 列出所有主机名 |
__fish_complete_pids | 列出所有进程 ID (PID) |
__fish_print_filesystems | 列出所有已知的文件系统 |
__fish_print_interfaces | 列出所有网络接口 |