Fish Shell

YUM安装 Fish2

yum -y install fish

RPM安装 Fish3

选择用于 RHEL 7 的软件包:fish-3.1.2-1.8.x86_64.rpm

rpm -ivh fish-3.1.2-1.8.x86_64.rpm

Pure.fish

将主题文件 fish_prompt.fish 拷贝至如下目录,重新登录即可

mkdir -p ~/.config/fish/functions
cd ~/.config/fish/functions

使用说明

????配置环境变量

Fish 环境变量保存在两个地方:

  1. ~/.config/fish/config.fish 用户环境变量
  2. /etc/fish/config.fish 全局环境变量
vim ~/.config/fish/config.fish
#在最后一行加入(注意目录间用空格隔开)
set -x PATH /opt/demo/bin /home/guest/bin $PATH
#最后重新加载fish即可

????配置别名(alias)

Fish中使用function来替代alias。定义的function存放于~/.config/fish/functions中,function名就是文件名,后缀为.fish,在fish启动的时候,所有位于functions文件夹里的以后缀.fish结尾的函数都会被自动加载。

config.fish文件中也可定义alias,等价于function。fish中的function不能在后台运行,即使加&也不能。

使用alias定义别名的基本示例:

# After fish 3.0 can use -s in shell (recommend)
alias -s l "ls -lah"

# Define alias in shell
alias rmi "rm -i"

# Define alias in config file
alias rmi="rm -i"

# This is equivalent to entering the following function:
function rmi
    rm -i $argv
end

# Then, to save it across terminal sessions:
funcsave rmi

每个函数都必须带参数 $argv,这是shell传过来的参数。

最后一条命令创建文件~/.config/fish/functions/rmi.fish

????切换默认shell为fish

echo $SHELL
cat /etc/shells
chsh -s /usr/bin/fish

????执行 Bash 脚本

bash -c SomeBashCommand

????命令代换

有别于bash中的`键,fish里采用括号来完成命令代换的功能,如

# in bash shell:
[root@urmylucky ~]# echo `date`
Tue Nov 3 13:53:55 CST 2020

# in fish shell:
❯ echo (date)
Tue Nov  3 13:58:03 CST 2020

fish对子命令也使用括号,例如:

for i in (ls)
    echo $i
end

????搜索历史命令

bash 中使用 ctrl+r 搜索历史命令,而在fish中,只需要键入想搜索的历史命令中的某些字母,再按ctrl+p就能不断搜索历史命令。

????采纳建议

->ctr+F 为采纳建议,Alt+-> 则只采纳部分建议。

????删除问候语

[fish2] vim ~/.config/fish/fishd.*
[fish3] vim ~/.config/fish/fish_variables
#将`SET fish_greeting:...`冒号之后的删除
SET fish_greeting:

四、????Fish Shell 语法

Fish 的语法非常自然,一眼就能看懂。

????if语句:

if grep fish /etc/shells
    echo Found fish
else if grep bash /etc/shells
    echo Found bash
else
    echo Got nothing
end

????switch语句:

switch (uname)
case Linux
    echo Hi Tux!
case Darwin
    echo Hi Hexley!
case FreeBSD NetBSD DragonFly
    echo Hi Beastie!
case '*'
    echo Hi, stranger!
end

????while循环:

while true
    echo "Loop forever"
end

????for循环:

for file in *.txt
    cp $file $file.bak
end

五、????函数

Fish 的函数用来封装命令,或者为现有的命令起别名。

function ll
    ls -lhG $argv
end

上面代码定义了一个ll函数。命令行执行这个函数以后,就可以用ll命令替代ls -lhG。其中,变量$argv表示函数的参数。

下面是另一个例子。

function ls
    command ls -hG $argv
end

上面的代码重新定义ls命令。注意,函数体内的ls之前,要加上command,否则会因为无限循环而报错。

六、????提示符

fish_prompt函数用于定义命令行提示符(prompt)。

function fish_prompt
    set_color purple
    date "+%m/%d/%y"
    set_color FF0
    echo (pwd) '>'
    set_color normal
end

执行上面的函数以后,你的命令行提示符就会变成下面这样。

02/06/13
/home/tutorial 

七、????配置

Fish 的配置文件是~/.config/fish/config.fish,每次 Fish 启动,就会自动加载这个文件。

我们可以在这个文件里面写入各种自定义函数,它们会被自动加载。比如,上面的fish_prompt函数就可以写在这个文件里面,这样每次启动 Fish,就会出现自定义的提示符。

Fish 还提供 Web 界面配置该文件。

fish_config

输入上面的命令以后,浏览器就会自动打开本机的 8000 端口,用户可以在网页上对 Fish 进行配置,比如选择提示符和配色主题。

相关文章:

  • 2021-11-13
  • 2021-08-19
  • 2021-12-28
  • 2021-03-31
  • 2022-01-26
  • 2021-12-07
  • 2021-11-04
  • 2021-06-13
猜你喜欢
  • 2022-12-23
  • 2021-10-11
  • 2022-12-23
  • 2021-12-01
  • 2021-11-23
  • 2021-09-25
相关资源
相似解决方案