Linux命令 fishshell - 比 bash 更好用的 shell

2021-11-11 09:17 更新

fishshell

比 bash 更好用的 shell

安裝

# Ubuntu 和 Debian 的安裝方法。
sudo apt-get install fish
# Mac 的安裝方法。
brew install fish

啟動(dòng)與幫助

由于 Fish 的語(yǔ)法與 Bash 有很大差異,Bash 腳本一般不兼容。因此,建議不要將 Fish 設(shè)為默認(rèn) Shell,而是每次手動(dòng)啟動(dòng)它。

# 安裝完成后,就可以啟動(dòng) Fish。
$ fish
# 使用過(guò)程中,如果需要幫助,可以輸入 help 命令
$ help

彩色顯示

# 無(wú)效命令為紅色
$ mkd
# 有效命令為藍(lán)色
$ mkdir
# 有效路徑會(huì)有下劃線。如果沒(méi)有下劃線,你就知道這個(gè)路徑不存在。
$ cat ~/somefi 

自動(dòng)建議

Fish 會(huì)自動(dòng)在光標(biāo)后面給出建議,表示可能的選項(xiàng),顏色為灰色。如果采納建議,可以按下 → 或 Control + F 。如果只采納一部分,可以按下 Alt + →。

$ /bin/hostname # 命令建議
$ grep --ignore-case # 參數(shù)建議
$ ls node_modules # 路徑建議

自動(dòng)補(bǔ)全

輸入命令時(shí),F(xiàn)ish 會(huì)自動(dòng)顯示匹配的上一條歷史記錄。如果沒(méi)有匹配的歷史記錄,F(xiàn)ish 會(huì)猜測(cè)可能的結(jié)果,自動(dòng)補(bǔ)全各種輸入。比如,輸入 pyt 再按下 Tab ,就會(huì)自動(dòng)補(bǔ)全為 python 命令。

Fish 還可以自動(dòng)補(bǔ)全 Git 分支。

腳本語(yǔ)法

if 語(yǔ)句

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

switch 語(yǔ)句

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 循環(huán)

while true
    echo "Loop forever"
end

for 循環(huán)

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

函數(shù)

Fish 的函數(shù)用來(lái)封裝命令,或者為現(xiàn)有的命令起別名。

function ll
    ls -lhG $argv
end

上面代碼定義了一個(gè) ll 函數(shù)。命令行執(zhí)行這個(gè)函數(shù)以后,就可以用 ll 命令替代 ls -lhG。其中,變量 $argv 表示函數(shù)的參數(shù)。

function ls
    command ls -hG $argv
end

上面的代碼重新定義 ls 命令。注意,函數(shù)體內(nèi)的 ls 之前,要加上 command,否則會(huì)因?yàn)闊o(wú)限循環(huán)而報(bào)錯(cuò)。

提示符

fish_prompt 函數(shù)用于定義命令行提示符(prompt)。

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

執(zhí)行上面的函數(shù)以后,你的命令行提示符就會(huì)變成下面這樣。

02/06/13
/home/tutorial > 

配置

Fish 的配置文件是 ~/.config/fish/config.fish,每次 Fish 啟動(dòng),就會(huì)自動(dòng)加載這個(gè)文件。Fish 還提供 Web 界面配置該文件。

$ fish_config # 瀏覽器打開 Web 界面配置

Running Commands: 兼容 bash 等shell的命令執(zhí)行方式

Getting Help: help/man cmd -> browser/terminal

Syntax Highlighting: 實(shí)時(shí)檢查命令是否正確

Wildcards: 支持縮寫 * 遞歸 匹配

Pipes and Redirections: 使用 ^ 代表 stderr

Autosuggestions: 自動(dòng)建議, 可以使用 Ctrl-f / -> 來(lái)補(bǔ)全

Tab Completions: 更強(qiáng)大的 tab 補(bǔ)全

Variables: 使用 set 設(shè)置

Exit Status: 使用 echo $status 替代 $?

Exports (Shell Variables)

Lists: all variables in fish are really lists

Command Substitutions: 使用 (cmd) 來(lái)執(zhí)行命令, 而不是 反引號(hào)、$()

Combiners (And, Or, Not): 不支持使用符合來(lái)表示邏輯運(yùn)算

Functions:使用 $argv 替代 $1

Conditionals (If, Else, Switch) / Functions / Loops: 更人性化的寫法(參考 py)

Prompt: function fish_prompt 實(shí)現(xiàn)

Startup (Where's .bashrc?): ~/.config/fish/config.fish,更好的方式是 autoloading-function、universal-variables

Autoloading Functions: ~/.config/fish/functions/.

Universal Variables:a variable whose value is shared across all instances of fish

set name 'czl' # 設(shè)置變量,替代 name=czl
echo $name
echo $status # exit status,替代 $?
env # 環(huán)境變量
set -x MyVariable SomeValue # 替代 export
set -e MyVariable
set PATH $PATH /usr/local/bin # 使用 lists 記錄 PATH
set -U fish_user_paths /usr/local/bin $fish_user_paths # 永久生效
touch "testing_"(date +%s)".txt" # command subtitution,替代 `date +%s`
cp file.txt file.txt.bak; and echo 'back success'; or echo 'back fail' # combiner
functions # 列出 fish 下定義的函數(shù)

參考資料


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)