13
1

More than 1 year has passed since last update.

Rust製のシェルNushellを使ってみる

Last updated at Posted at 2022-01-05

Macを新調したついでにzshfishとは違うシェルはないかと探していたところ、Rust製のシェルnushellを見つけたので試してみました。

インストール

homebrewでインストールできます。

brew install nushell

起動

nuコマンドで起動します。

nu

Welcome to Nushell 0.41.0 (type 'help' for more info)
> 

nushellはまだ開発中のためtcshで標準shellにするのはお勧めしないとのことで、標準シェルにするのはやめておきます。

多くのunix系コマンドがnushell組込み

macOSでlsの結果を日付で昇順にソートしてわかりやすいサイズ表示にするにはls -ltrhオプションが必要です。しかしながら、nushellでこれを実行すると

> ls -ltrh

error: ls unexpected -ltrh
   ┌─ shell:15:7
   │
15 │ ls -ltrh
   │       ^ unexpected flag (try ls -h)

と表示されエラーになります。標準のlsじゃないの?と思ってwhich lsすると、

> which ls

───┬─────┬──────────────────────────┬─────────
 # │ arg │           path           │ builtin 
───┼─────┼──────────────────────────┼─────────
 0 │ ls  │ Nushell built-in commandtrue    
───┴─────┴──────────────────────────┴─────────

と表示されます。

そうです、nushellでは他のShellで標準的なコマンドが組込みかつ独自仕様となっています。

ツール化されたコマンドライン引数

ls -hでヘルプを見ると

> ls -h
略
Flags:
  -h, --help: Display this help message
  -a, --all: Show hidden files
  -l, --long: List all available columns for each entry
  -s, --short-names: Only print the file names and not the path
  -d, --du: Display the apparent directory size in place of the directory metadata size

とオプションが5つしかありません。

ls -ltrhのようにソートするには、オプションではなくsort-byコマンドを使います。

> ls | sort-by modified

───┬───────────┬──────┬────────┬────────────────
 # │   name    │ type │  size  │    modified    
───┼───────────┼──────┼────────┼────────────────
 0 │ Music     │ Dir  │   96 B │ 2 days ago     
 1 │ Desktop   │ Dir  │   96 B │ 2 days ago     
 2 │ Public    │ Dir  │  128 B │ 2 days ago     
 3 │ Pictures  │ Dir  │  128 B │ 2 days ago     
 4 │ Movies    │ Dir  │  128 B │ 2 days ago     
 5 │ Library   │ Dir  │ 2.4 KB │ 3 hours ago    
 6 │ Downloads │ Dir  │  128 B │ 3 hours ago    
 7 │ mydev     │ Dir  │   64 B │ 23 minutes ago 
 8 │ Documents │ Dir  │  128 B │ 18 minutes ago 
───┴───────────┴──────┴────────┴────────────────

同じような結果が得られました。

型のついた出力結果

さらに出力結果を以下のように内容によって絞り込むことが可能です。

> ls | sort-by modified | where size > 1KB
───┬─────────┬──────┬────────┬────────────
 # │  name   │ type │  size  │  modified  
───┼─────────┼──────┼────────┼────────────
 0 │ Library │ Dir  │ 2.4 KB │ 3 hours ago
───┴─────────┴──────┴────────┴────────────

nushellはコマンド実行結果に型がついているため、このような処理が簡単に行えます。

通常のshellコマンドだと結果は全てテキストであり、結果を操作したい場合はawkなどでテキスト処理するしかありませんでしたが、nushellではこのようなことが行えます。

割愛しますがJSON、Toml、CSVなどの処理も簡単です。

ローカルのlsを使うには

^ls

のように^をつければnushell組み込みだけでなくローカルのコマンドも使えます。

その他

他にも以下のような機能が色々と用意されていて、nushell bookにまとめられています。

  • Pluginシステム
  • CSVなどの構造化データを高速処理するためのDataframe

まとめ

  • 標準shell化は非推奨
  • unix系コマンドは組み込みかつ独自仕様
  • 結果に型がありawkなどでテキスト処理する必要がない

というわけで、常用するかというと微妙ですが何かの機会に使ってみようかと思います。

13
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
1