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

2021-11-11 09:17 更新

fishshell

比 bash 更好用的 shell

安裝

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

啟動與幫助

由于 Fish 的語法與 Bash 有很大差異,Bash 腳本一般不兼容。因此,建議不要將 Fish 設為默認 Shell,而是每次手動啟動它。

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

彩色顯示

# 無效命令為紅色
$ mkd
# 有效命令為藍色
$ mkdir
# 有效路徑會有下劃線。如果沒有下劃線,你就知道這個路徑不存在。
$ cat ~/somefi 

自動建議

Fish 會自動在光標后面給出建議,表示可能的選項,顏色為灰色。如果采納建議,可以按下 → 或 Control + F 。如果只采納一部分,可以按下 Alt + →。

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

自動補全

輸入命令時,F(xiàn)ish 會自動顯示匹配的上一條歷史記錄。如果沒有匹配的歷史記錄,F(xiàn)ish 會猜測可能的結果,自動補全各種輸入。比如,輸入 pyt 再按下 Tab ,就會自動補全為 python 命令。

Fish 還可以自動補全 Git 分支。

腳本語法

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

while true
    echo "Loop forever"
end

for 循環(huán)

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

函數(shù)

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

function ll
    ls -lhG $argv
end

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

function ls
    command ls -hG $argv
end

上面的代碼重新定義 ls 命令。注意,函數(shù)體內(nèi)的 ls 之前,要加上 command,否則會因為無限循環(huán)而報錯。

提示符

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ù)以后,你的命令行提示符就會變成下面這樣。

02/06/13
/home/tutorial > 

配置

Fish 的配置文件是 ~/.config/fish/config.fish,每次 Fish 啟動,就會自動加載這個文件。Fish 還提供 Web 界面配置該文件。

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

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

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

Syntax Highlighting: 實時檢查命令是否正確

Wildcards: 支持縮寫 * 遞歸 匹配

Pipes and Redirections: 使用 ^ 代表 stderr

Autosuggestions: 自動建議, 可以使用 Ctrl-f / -> 來補全

Tab Completions: 更強大的 tab 補全

Variables: 使用 set 設置

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

Exports (Shell Variables)

Lists: all variables in fish are really lists

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

Combiners (And, Or, Not): 不支持使用符合來表示邏輯運算

Functions:使用 $argv 替代 $1

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

Prompt: function fish_prompt 實現(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' # 設置變量,替代 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)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號