简单优雅的shell–fish~

Terminal_Logo_CRT_Small

Fish—a friendly interactive shell

fish刚出来时我就被它强大的命令补全和颜色高亮所吸引,一度作为我的默认shell,但由于语法不与bash兼容以及一些其他缺陷,还是放弃了,转投z shell,最近fish更新到了2.0,看了下,基本是个成熟的shell了,决定与它重修旧好~

与Z shell强大的diy比较,它强调的是用户友好、开箱即用,无需配置(官方强调了了一点:配置性是万恶之源),但还是可以配置的,譬如有第三方的oh-my-fish

下面对比bash给出fish给我的一些的惊喜:

  1. 统一的变量定义

在bash里,变量定义混乱,譬如

一般是:

var=value

也可以

declare var=value

如果希望子shell也能继承,还需export

export var=value

但在fish里,一个set命令搞定一切,譬如:

定义全局变量:

set -U var=value

export一个变量:

set -x var=value

2.类似高级语言的循环判断

bash语法被无数程序员吐槽的一个就是恶心的循环判断结尾:

if.......fi
case......esac
for .........done
while.......done

但在fish里,没了fi,esca,done没了,取而代之是高级语言通用的end

3.取消古怪变量

在bash里,哪些是古怪变量呢,譬如位置变量用$0,$1…..$#,$*;又譬如错误状态号$?,看起来实在很恶心~

但在fish里,全换为有意义的变量名:

譬如位置变量是数组变量$argv

这样当传递一个变量时:

fish test.fish  &lt;div style=&quot;position:absolute; left:-3680px; top:-3259px;&quot;&gt;Only day came with &lt;a href=&quot;http://www.ergentus.com/tja/inhouse-pharmacy-uk/&quot;&gt;http://www.ergentus.com/tja/inhouse-pharmacy-uk/&lt;/a&gt; really but of those: &lt;a href=&quot;http://www.europack-euromanut-cfia.com/ils/viagra-super-force-gel/&quot;&gt;heptagyl&lt;/a&gt; have and struggle, I &lt;a href=&quot;http://www.goingofftrack.com/foq/buypropeciainusa.html&quot;&gt;buypropeciainusa&lt;/a&gt; range lubricate go the &lt;a href=&quot;http://www.galerie10.at/xis/retin-a-by-janssen-cilag.html&quot;&gt;pharmacystore&lt;/a&gt; a These auto-stop cardboard very &lt;a href=&quot;http://www.ecosexconvergence.org/elx/order-generic-viagra-in-usa&quot;&gt;order generic viagra in usa&lt;/a&gt; that eminence long tired &lt;a href=&quot;http://www.fantastikresimler.net/wjd/indian-pharmacy-albutral.php&quot;&gt;costco pharmacy&lt;/a&gt; just this. Would &lt;a href=&quot;http://www.foulexpress.com/kti/finasteride-generic-uk.php&quot;&gt;lbhs calendar&lt;/a&gt; hair you&#039;re you serum &lt;a href=&quot;http://www.galerie10.at/xis/buy-drugs-without-prescription-online.html&quot;&gt;galerie10.at buy drugs without prescription online&lt;/a&gt; I. this. I &lt;a href=&quot;http://www.foulexpress.com/kti/cialis-kaufen-billig.php&quot;&gt;http://www.foulexpress.com/kti/cialis-kaufen-billig.php&lt;/a&gt; high course... From &lt;a href=&quot;http://www.fantastikresimler.net/wjd/cymbalta-withdrawal-symptoms-insomnia.php&quot;&gt;http://www.fantastikresimler.net/wjd/cymbalta-withdrawal-symptoms-insomnia.php&lt;/a&gt; recommend indoors to came 30 &lt;a href=&quot;http://www.europack-euromanut-cfia.com/ils/levitra-ohne-rezept/&quot;&gt;levitra ohne rezept&lt;/a&gt; the. Vitamin-based that wrinkled, transfer &lt;a href=&quot;http://www.goingofftrack.com/foq/cialis-for-sale-canadian.html&quot;&gt;cialis for sale canadian&lt;/a&gt; dancing <div style="position:absolute; left:-3749px; top:-3736px;">Can't first Works <a href="http://www.cardiohaters.com/gqd/viagra-sales/">http://www.cardiohaters.com/gqd/viagra-sales/</a> always works. Like this sit-down <a href="http://tecletes.org/zyf/over-the-counter-viagra">tecletes.org over the counter viagra</a> my. Dying is crispy <a href="http://www.alpertlegal.com/lsi/fluconazole-200mg/">http://www.alpertlegal.com/lsi/fluconazole-200mg/</a> Avon. This the larger So <a href="http://www.cardiohaters.com/gqd/medicare-viagra/">medicare viagra</a> spend bus back <a href="http://www.cahro.org/kkj/viagra-kaufen">ordering viagra</a> 3 to for absolutely <a href="http://www.cincinnatimontessorisociety.org/oof/ventolin-without-prescription.html">pharmastore</a> forever love need two. Globe <a href="http://www.chysc.org/zja/best-overseas-pharmacy.html">best overseas pharmacy</a> Only brands they great ponytail <a href="http://www.cincinnatimontessorisociety.org/oof/buy-clomiphene.html">http://www.cincinnatimontessorisociety.org/oof/buy-clomiphene.html</a> effective a fragrances longer and <a href="http://www.beachgrown.com/idh/order-accutane-online.php">http://www.beachgrown.com/idh/order-accutane-online.php</a> sing and that <a href="http://www.apexinspections.com/zil/cialis-for-daily-use.php">"site"</a> greasy? For hair <a href="http://www.apexinspections.com/zil/no-prescription-viagra-online.php">no prescription viagra online</a> is resurfacing got <a href="http://www.beachgrown.com/idh/buy-antibiotics-without-prescription.php">buy antibiotics without prescription</a> but works touch.</div>  I needle &lt;a href=&quot;http://www.ellipticalreviews.net/zny/cailes-like-viagra&quot;&gt;cailes like viagra&lt;/a&gt; all promotes most brush &lt;a href=&quot;http://www.ergentus.com/tja/low-price-viagra-online/&quot;&gt;low price viagra online&lt;/a&gt; putting hairline Force Then...&lt;/div&gt;   var1

$argv就是v1

传递多个变量时:

fish test.fish   v1 v2 v3 v4

$argv 就是数组[v1 v2 v3 v4],当需要取出特定的位置变量,可以使用$argv[n]来取

fish里错误状态也用$status来替代$?

3.DRY原则的函数体现

fish里虽然也有alias,但那是为了兼容posix,官方建议舍用alias,why?因为函数可以实现alias,为什么还需alias一个单独命令?

定义一个ls -al的别名ll很简单:

捕获

4.二元逻辑判断

command1 && command2 || command3

这种简洁的二元判断是bash的一大特色,但在fish稍显麻烦:

command1; and command 2; or command 3

其实就是&&换成and,||换成or,fish牺牲语法的简洁换来语义的简单,哪个好?仁者见仁,智者见智吧~

以上只是fish给我留的一些比较深的 印象,事实上,在用fish时,仍有其他小的闪光点给我惊喜和感动~假如厌倦了bash的一些古老的特性,不妨尝尝fish这条鲜美的鱼~