W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
比 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 grep fish /etc/shells
echo Found fish
else if grep bash /etc/shells
echo Found bash
else
echo Got nothing
end
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 true
echo "Loop forever"
end
for file in *.txt
cp $file $file.bak
end
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ù)
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: