Cheat Sheet速查表
跳转到主要内容

Fish shell 速查表

本文根据 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列出所有网络接口