侧边栏壁纸
Linux入门自学网博主等级

每日学一条Linux命令,终成Linux大神

  • 累计撰写 725 篇文章
  • 累计创建 143 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Linux fishshell命令教程:如何高效利用用户友好交互式Shell(附实例详解和注意事项)

Linux fishshell命令介绍

fishshell命令是用来启动fish shell的命令,fish shell是一个友好的交互式shell,它可以让你的命令行更加高效,有用,和有趣,而不需要学习一堆复杂的语法和配置选项。

Linux fishshell命令适用的Linux版本

fishshell命令适用于大多数Linux发行版,比如Ubuntu,Debian,Fedora,CentOS,Arch Linux等。

如果你的Linux系统没有安装fish shell,你可以使用以下命令来安装:

  • Ubuntu或Debian:[linux@bashcommandnotfound.cn ~]$ sudo apt install fish
  • Fedora:[linux@bashcommandnotfound.cn ~]$ sudo dnf install fish
  • CentOS 7:[linux@bashcommandnotfound.cn ~]$ sudo yum install fish
  • CentOS 8:[linux@bashcommandnotfound.cn ~]$ sudo dnf install fish
  • Arch Linux:[linux@bashcommandnotfound.cn ~]$ sudo pacman -S fish

如果你想把fish shell设为你的默认shell,你可以使用以下命令:

[linux@bashcommandnotfound.cn ~]$ chsh -s /usr/bin/fish

Linux fishshell命令的基本语法

fishshell命令的基本语法是:

fishshell [options] [file]

其中,options是可选的参数,file是可选的脚本文件。

如果没有指定任何参数或文件,fishshell命令会启动一个交互式的fish shell,让你输入命令并执行。

Linux fishshell命令的常用选项或参数说明

fishshell命令的常用选项或参数有:

选项或参数说明
-c, --command=COMMAND执行COMMAND,然后退出
-d, --debug-level=LEVEL设置调试级别为LEVEL,范围是0到5,0表示关闭调试,5表示最详细的调试信息
-h, --help显示帮助信息,然后退出
-i, --interactive强制启动交互式的fish shell,即使有文件或命令参数
-l, --login以登录shell的方式启动fish shell,会执行一些额外的初始化操作,比如读取/etc/profile文件
-n, --no-execute不执行任何命令或文件,只检查语法错误
-p, --profile=FILE记录每条命令的执行时间到FILE文件中,用于性能分析
-v, --version显示fish shell的版本信息,然后退出

Linux fishshell命令的实例

实例1:启动交互式的fish shell

如果你想启动一个交互式的fish shell,你可以直接输入fishshell命令,不需要任何参数或文件:

[linux@bashcommandnotfound.cn ~]$ fishshell

你会看到一个类似这样的提示符:

you@hostname ~>

这表示你已经进入了fish shell,你可以输入任何你想要执行的命令,比如:

you@hostname ~> echo hello fish

hello fish

实例2:执行一条命令,然后退出

如果你只想执行一条命令,然后退出fish shell,你可以使用-c或--command选项,后面跟上你想要执行的命令,比如:

[linux@bashcommandnotfound.cn ~]$ fishshell -c "echo hello fish"

hello fish

注意,你需要用双引号把命令括起来,以防止命令中有空格或特殊字符被解释为fishshell命令的参数。

实例3:执行一个脚本文件,然后退出

如果你有一个包含多条命令的脚本文件,你可以使用fishshell命令来执行它,只需要把文件名作为参数传递给fishshell命令,比如:

[linux@bashcommandnotfound.cn ~]$ fishshell myscript.fish

这会执行myscript.fish文件中的所有命令,然后退出fish shell。

注意,你的脚本文件需要有可执行权限,你可以用chmod命令来设置,比如:

[linux@bashcommandnotfound.cn ~]$ chmod +x myscript.fish

另外,你的脚本文件的第一行需要指定fish shell作为解释器,比如:

#!/usr/bin/fish

实例4:以登录shell的方式启动fish shell

如果你想以登录shell的方式启动fish shell,你可以使用-l或--login选项,比如:

[linux@bashcommandnotfound.cn ~]$ fishshell -l

这会执行一些额外的初始化操作,比如读取/etc/profile文件,设置一些环境变量,等等。

你可以用echo $0命令来检查你是否以登录shell的方式启动了fish shell,如果是的话,你会看到一个类似这样的输出:

-fish

注意,输出中有一个减号(-)作为前缀,表示登录shell。

实例5:检查一个脚本文件的语法错误,不执行

如果你想检查一个脚本文件是否有语法错误,但不想执行它,你可以使用-n或--no-execute选项,比如:

[linux@bashcommandnotfound.cn ~]$ fishshell -n myscript.fish

这会检查myscript.fish文件中的语法错误,如果有错误,会显示在终端上,如果没有错误,不会有任何输出。

实例6:显示fish shell的版本信息,然后退出

如果你想查看你的fish shell的版本信息,你可以使用-v或--version选项,比如:

[linux@bashcommandnotfound.cn ~]$ fishshell -v

这会显示类似这样的输出:

fish, version 3.6.4

然后退出fish shell。

实例7:设置调试级别,然后启动交互式的fish shell

如果你想在启动fish shell时设置调试级别,以便查看更多的内部信息,你可以使用-d或--debug-level选项,后面跟上一个数字,表示调试级别,范围是0到5,0表示关闭调试,5表示最详细的调试信息,比如:

[linux@bashcommandnotfound.cn ~]$ fishshell -d 3

这会启动一个交互式的fish shell,并显示一些调试信息,比如:

fish: Exec job 'fishshell -d 3' with id 1
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Exec job 'fishshell -d 3' with id 1
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
fish: Set status of job 1 to 0 using short circuit
fish: Job is constructed
fish: Job is not in foreground
you@hostname ~>

你可以根据你的需要,选择不同的调试级别,以获取不同的调试信息。

实例8:记录每条命令的执行时间到一个文件中,用于性能分析

如果你想记录每条命令的执行时间到一个文件中,以便进行性能分析,你可以使用-p或--profile选项,后面跟上一个文件名,比如:

[linux@bashcommandnotfound.cn ~]$ fishshell -p profile.txt

这会启动一个交互式的fish shell,并把每条命令的执行时间记录到profile.txt文件中,文件的格式是:

time (in microseconds) - cumulative time - command

例如:

1000 - 1000 - echo hello
2000 - 3000 - sleep 1
500 - 3500 - echo world

你可以用这个文件来分析你的命令的性能,找出哪些命令花费了较多的时间,或者哪些命令可以优化。

实例9:启动一个网页界面,让你配置fish shell的外观和行为

如果你想启动一个网页界面,让你配置fish shell的外观和行为,比如主题,颜色,提示符,绑定,缩写,等等,你可以使用fish_config命令,比如:

[linux@bashcommandnotfound.cn ~]$ fish_config

这会启动一个本地的web服务器,并打开你的默认浏览器,显示一个类似这样的网页:

![fish_config]

你可以在这个网页上选择不同的选项,查看效果,并应用到你的fish shell中。³

实例10:显示你在键盘上按下的键的信息

如果你想显示你在键盘上按下的键的信息,比如键码,字符,绑定,等等,你可以使用fish_key_reader命令,比如:

[linux@bashcommandnotfound.cn ~]$ fish_key_reader

这会启动一个程序,等待你输入键盘上的键,并显示相关的信息,比如:

Press a key

          hex:   1B  char: \c[  (or \e)

bind: set_color --bold yellow; echo -n (date +%m-%d-%y); set_color normal

          hex:   4F  char: O

bind: execute

          hex:   50  char: P

bind: 'do nothing'

这表示你按下了Esc键,然后按下了OP键,分别显示了它们的十六进制码,字符,和绑定的动作。²

你可以用这个命令来测试你的键盘,或者查看你的键盘绑定是否正确。

Linux fishshell命令的注意事项

  • fishshell命令不是一个标准的POSIX兼容的shell,它有自己的语法和特性,与其他常见的shell,比如bash或zsh,有一些不同。
  • fishshell命令不支持一些传统的shell特性,比如历史替换,命令行编辑模式,或者一些特殊的变量,比如$?或$$。如果你想使用这些特性,你可能需要使用其他的shell,或者找到fish shell的替代方案。
  • 如果你的Linux系统没有安装fish shell,你可能会看到这样的错误信息:

bash: fishshell: command not found

这表示你的系统找不到fishshell命令,你需要先安装fish shell,然后再使用fishshell命令。

0

评论区