nushell

提供: ArchWiki
ナビゲーションに移動 検索に移動

nushellは、新しいタイプのシェルです。配列、テーブル、レコード、数値/ブール型などの構造化・型付けされたデータをネイティブにサポートし、シェルのようなワークフローで様々なデータ型の問い合わせ、フィルタ、ソート、変換、その他の操作を容易にする構文とビルトインを提供し、多くのビルトインまたはユーザー定義フォーマットでの入力と出力をサポートします。

インストール

nushell パッケージをインストールします。

機能概要

構造化データのサポート

nushell のビルトインは複雑なデータ型を理解し出力します。例えば、ls ビルトインは、name, size, type という属性を持つファイルを表す項目の配列を出力する。(file, dir, symlink, etc.), modified などの属性を持ちます。

また、従来のシェルのようにテキストとしてではなく、データとして問い合わせたり操作したりするためのビルトインが多数提供されており、シェルらしいワークフローが実現されている。例えば、where ビルトインは、配列やテーブルの中身をフィルタリングすることができます:

$ ls | where type == file | where size > 10mb

stdinで受信したデータをフィルタリングし、stdoutで出力するため、上記のように複数のフィルタを連鎖させることができる。もう一つの例:

$ ls | where type == file | get size | sum

get ビルトインは、オブジェクトから属性にアクセスするために使用されます。この例では、stdin 経由でオブジェクトの配列を与えているので、配列の各要素の size プロパティを取得し、数値の配列を生成しています。最後に、その配列を sum に渡して、それらの合計を計算させます。

出力

nushell は極めて多彩な出力機能を備えています。箱から出してすぐに、カラー、アスキーアート、詳細なエラーメッセージを備えたモダンな外観を誇っています。

テキストで出力する

デフォルトでは、ls のようなコマンドの出力は、列挙された行を持つ色付きの ASCII テーブルとして表示されます。(構造化オブジェクトの配列を生成する) のようなコマンドの出力は、各ファイルが行、各属性が列である列挙された行を持つ色付きの ASCII テーブルとして表示されます。例:

/usr/lib> ls | where name =~ ".*(alsa|pulse|pipewire).*" | first 10
╭───┬────────────────────────────┬─────────┬──────────┬──────────────╮
│ # │            name            │  type   │   size   │   modified   │
├───┼────────────────────────────┼─────────┼──────────┼──────────────┤
│ 0 │ alsa-lib                   │ dir     │   4.1 KB │ 2 days ago   │
│ 1 │ alsa-topology              │ dir     │   4.1 KB │ 3 months ago │
│ 2 │ libdrumstick-alsa.so       │ symlink │     22 B │ 7 months ago │
│ 3 │ libdrumstick-alsa.so.2     │ symlink │     26 B │ 7 months ago │
│ 4 │ libdrumstick-alsa.so.2.7.2 │ file    │ 335.3 KB │ 7 months ago │
│ 5 │ libgvncpulse-1.0.so        │ symlink │     21 B │ 9 months ago │
│ 6 │ libgvncpulse-1.0.so.0      │ symlink │     25 B │ 9 months ago │
│ 7 │ libgvncpulse-1.0.so.0.0.1  │ file    │  14.1 KB │ 9 months ago │
│ 8 │ libpipewire-0.3.so         │ symlink │     20 B │ 2 weeks ago  │
│ 9 │ libpipewire-0.3.so.0       │ symlink │     26 B │ 2 weeks ago  │
╰───┴────────────────────────────┴─────────┴──────────┴──────────────╯

データ形式での出力

Data can also be output in any various data formats, including JSON, YAML, TOML, HTML, XML, SQL, CSV, Markdown tables, and others. The user can also define their own custom viewers to support arbitrary data types.

To output data in a given format, simply pipe the data to to FORMAT:

/usr/lib> ls | where name =~ ".*alsa.*" | first 3 | to yaml
- name: alsa-lib
  type: dir
  size: 4096
  modified: 2023-05-03 16:04:35.544273606
- name: alsa-topology
  type: dir
  size: 4096
  modified: 2023-01-13 19:29:45.179245376
- name: libdrumstick-alsa.so
  type: symlink
  size: 22
  modified: 2022-10-02 13:28:57

To save the output of a command in a file, pipe it to the save builtin:

> ls | to json | save my-file.json

If the output file already exists, save will refuse to overwrite it. You can force overwriting files using the -f switch.

Note that nushell defaults to producing pretty-printed JSON. To output JSON without pretty-printing, use to json --raw.

See to --help for a list of supported formats.

エラー出力

nushell prints colorized and detailed error messages that pinpoint the exact source of the error and suggest solutions. Example error message:

> ls -a --never-gonna-give-you-up /tmp
Error: nu::parser::unknown_flag

 × The `ls` command doesn't have flag `never-gonna-give-you-up`.
   ╭─[entry #24:1:1]
 1 │ ls -a --never-gonna-give-you-up /tmp
   ·       ────────────┬────────────
   ·                   ╰── unknown flag
   ╰────
  help: Available flags: --help(-h), --all(-a), --long(-l),
        --short-names(-s), --full-paths(-f), --du(-d), --directory(-D),
        --mime-type(-m). Use `--help` for more information.

入力

データファイルからの入力

nushell has native support for reading data in various formats, including JSON, YAML, TOML, SQL, HTML/XML, and others, allowing the user to utilize its powerful data querying and manipulation capabilities on data read from any file format. The user can also add support for new formats by adding plugins.

Data is read from files using the open builtin. For example, assuming we have a file movies.yaml with the following contents:

- movie: Matrix
  genre: Action
- movie: Lord of the Rings
  genre: [Action, Fantasy]
- movie: Independence Day
  genre: [Action, Sci-Fi]

Then executing open movies.yaml would produce the following output:

╭───┬───────────────────┬─────────────────╮
│ # │       movie       │      genre      │
├───┼───────────────────┼─────────────────┤
│ 0 │ Matrix            │ Action          │
│ 1 │ Lord of the Rings │ ╭───┬─────────╮ │
│   │                   │ │ 0 │ Action  │ │
│   │                   │ │ 1 │ Fantasy │ │
│   │                   │ ╰───┴─────────╯ │
│ 1 │ Independence Day  │ ╭───┬─────────╮ │
│   │                   │ │ 0 │ Action  │ │
│   │                   │ │ 1 │ Sci-Fi  │ │
│   │                   │ ╰───┴─────────╯ │
╰───┴───────────────────┴─────────────────╯

Input from external programs

For external programs, which typically produce their output as plain text, nushell offers the ability to parse their output and convert it into a structured datatype, so that the user can utilize nushell's full native data processing capabilities even on arbitrary output generated by agnostic programs.

Using parse with regular expression

Parsing external program output can be easily performed using the parse builtin. One typical workflow is using the -r switch, which tells parse to use a regular expression for extracting fields out of each line of text in the input. For example, to parse the output of pacman's -Si command, one might do something like this:

> pacman -Si rclone | parse -r '(?P<name>.*\w) +: (?P<value>.+)'
╭────┬────────────────┬─────────────────────────────────────────────────╮
│  # │      name      │                    value                        │
├────┼────────────────┼─────────────────────────────────────────────────┤
│  0 │ Repository     │ extra                                           │
│  1 │ Name           │ rclone                                          │
│  2 │ Version        │ 1.62.2-1                                        │
│  3 │ Description    │ Sync files to and from Google Drive, S3, Swift, │
│    │                │ Cloudfiles, Dropbox and Google Cloud Storage    │
│  4 │ Architecture   │ x86_64                                          │
│  5 │ URL            │ https://rclone.org/                             │
│  6 │ Licenses       │ MIT                                             │
│  7 │ Groups         │ None                                            │
│  8 │ Provides       │ None                                            │
│  9 │ Depends On     │ glibc                                           │
│ 10 │ Optional Deps  │ fuse2: for rclone mount                         │
│ 11 │ Conflicts With │ None                                            │
│ 12 │ Replaces       │ None                                            │
│ 13 │ Download Size  │ 18.12 MiB                                       │
│ 14 │ Installed Size │ 75.93 MiB                                       │
│ 15 │ Packager       │ Morten Linderud <foxboron@archlinux.org>        │
│ 16 │ Build Date     │ Sun 02 Apr 2023 14:09:44 EEST                   │
│ 17 │ Validated By   │ MD5 Sum  SHA-256 Sum  Signature                 │
╰────┴────────────────┴─────────────────────────────────────────────────╯

nushell uses a Perl-like regular expression syntax, which is provided by the Regex crate (of the Rust programming language). The syntax is described in the crate's documentation.

Using parse with template string

Another way, which does not require regular expression knowledge, is by omitting the -r switch and providing parse with a template string. However, depending on the case, it may necessitate extra steps for pre- and post-processsing the results. For example:

> pacman -Si just | lines | parse '{field} : {value}' | str trim

Here we performed the same task as the above, but with three differences. Aside from using a template string rather than regular expression (i.e. without the -r switch), the first notable difference is that when parse is given a a template strings, it acts on the entirety of its input rather than on each line in it. So in order to parse the input on a per-line basis, we must first split it into an array of lines using the lines builtin.

The second difference is that template strings do not automatically trim surplus whitespace, which results in the matched fields containing all extra whitespace surrounding them, so we had to post-process the output with str trim, which conveniently acts on all fields for all items in the provided array.

Data manipulation

Conversion

The combination of its open and save builtins allow it to be easily used to convert data files across any supported formats. For example:

The data can then be freely manipulated or converted by piping it to other commands. For example:

> open movies.yaml | first 2 | to json --raw
[{"movie": "Matrix","genre": "Action"},{"movie": "Lord of the Rings","genre":["Action","Fantasy"]}]
> open movies.yaml | first 2 | to json --raw | save movies.json
> cat movies.json
(same output as above)

Comparison with traditional shells

この記事またはセクションは加筆を必要としています。
理由: Needs to elaborate on some of the points enumerated in the first paragraph. (議論: トーク:Nushell#)

nushell is not a POSIX shell, and has significant differences from traditional/POSIX-compatible shells in various ways, including its syntax, supported builtins, the way builtins work, the command-line options they accept, the type of data they consume and produce, and so on.

Syntax

Redirecting output to a file cannot be done using > like in other shells. Instead, you must use the save builtin. If the output in question happens to be a complex data structure (like the output of its builtin ls), then you must first serialize it into a textual representation using the to builtin.

So rather than:

ls > file.txt

You must use:

ls | to yaml | save file.yaml

参照

  • Nushell's Cookbook nushell の様々なカテゴリからタスクを実行する方法についての例が含まれています。