Nushell

60 readers
1 users here now

What is Nushell?

Nushell is a powerful shell and scripting language with strong typing, querying, and piping functionalities.

From the official documentation; Introduction:

The goal of this project is to take the Unix philosophy of shells, where pipes connect simple commands together, and bring it to the modern style of development. Thus, rather than being either a shell, or a programming language, Nushell connects both by bringing a rich programming language and a full-featured shell together into one package.

Nu takes cues from a lot of familiar territory: traditional shells like bash, object based shells like PowerShell, gradually typed languages like TypeScript, functional programming, systems programming, and more. But rather than trying to be a jack of all trades, Nu focuses its energy on doing a few things well:

Examples

Shell Examples…

0x[7F000001] | chunks 1 | each { into int } | str join "."
# => 127.0.0.1
ls | where size > 1kb
# => ╭───┬───────────────────┬──────┬─────────┬────────────╮
# => │ # │       name        │ type │  size   │  modified  │
# => ├───┼───────────────────┼──────┼─────────┼────────────┤
# => │ 0 │ Gemfile           │ file │ 1.1 KiB │ 3 days ago │
# => │ 1 │ Gemfile.lock      │ file │ 6.9 KiB │ 3 days ago │
# => │ 2 │ LICENSE           │ file │ 1.1 KiB │ 3 days ago │
# => │ 3 │ SUMMARY.md        │ file │ 3.7 KiB │ 3 days ago │
# => ╰───┴───────────────────┴──────┴─────────┴────────────╯
help commands | where name == each | first | get params.name
# => ╭───┬──────────────────╮
# => │ 0 │ closure          │
# => │ 1 │ --help(-h)       │
# => │ 2 │ --keep-empty(-k) │
# => ╰───┴──────────────────╯
http get https://programming.dev/api/v3/site | get all_languages | length
# => 184

http get https://programming.dev/api/v3/site | get taglines.content
# => ╭───┬────────────────────────────────────╮
# => │ 0 │ Broken communities have been fixed │
# => ╰───┴────────────────────────────────────╯

Web-Links

founded 3 months ago
MODERATORS
1
 
 

My website is implemented through Hugo, with content sources in Markdown. Metadata is added through a so-called "front matter" header within Markdown files. I noticed date metadata (front matter) was missing on content pages and consequently had 2001 on RSS feeds.

I used Nushell to en-mass add page dates after-the-fact, with date values determined through Git.

# Determine pages with missing date front matter (may be missing pages that have `date = ` as content)
glob **/*.md | where {|x| $x | open | not ($in | str contains 'date = ') } | save missing.json

# Determine content creation dates through Git add authoring date
open missing.json | wrap path | upsert date {|x| git log '--follow' '--diff-filter=A' '--format=%ad' '--date=iso' '--' $x.path | into datetime } | save dates.json

# Prepend date TOML front matter to closing fence
open dates.json | each {|x| $x.path | open --raw | str replace "\r\n+++\r\n" $"\r\ndate = \"($x.date | format date '%Y-%m-%d %H:%M:%S')\"\r\n+++\r\n" | collect | save -f $x.path }

Example [inline] result/fixup:

date = "2022-08-07 18:31:18"
+++

Some work details and manual cleanup (e.g. pages with resource front matter where the date declaration must be placed before them) omitted.

2
 
 

I added two solutions to the Rosetta Code FizBuzz page in Nu.

The one that was already there was quite confusing/non-intuitive to me; with string determination, and by index value fixups.

I like the match solution because it's structurally obvious and demonstrates the record-field-value match and logical case matching:

1..100 | each {
  { x: $in, mod3: ($in mod 3), mod5: ($in mod 5), }
  | match $in {
    { mod3: 0, mod5: 0, } => 'FizzBuz',
    { mod3: 0, mod5: _, } => 'Fizz',
    { mod3: _, mod5: 0, } => 'Buzz',
                        _ => $in.x
  }
} | str join "\n"

Do you have alternative suggestions or improvements?

3
9
submitted 2 weeks ago by Kissaki to c/nushell
 
 

Nushell is a powerful shell and scripting language with strong typing, querying, and piping functionalities.

This release adds runtime pipeline input type checking, several new commands and operators, and various other miscellaneous improvements.

4
3
submitted 1 month ago* (last edited 1 month ago) by Kissaki to c/nushell
 
 

I track and version my Nushell environment and configuration in a public repository.

I added a GitHub Actions workflow that tests these files. That will ensure a more defined environment and prerequisites/assumptions, given that they have to be set up in the workflow configuration. Given that I mostly work on Windows, but set up the CI to run on Linux/Ubuntu, it will also ensure platform neutrality.


Since Nushell version 0.101.0, there's no need for a default, base env.nu or config.nu, and the nu binary can be called with only the custom, minimal env and config files.

The nu binary offers --env-config and --config parameters.

I noticed that when using them, errors do not lead to error exist codes; nu will continue execution and report success despite env or config not loading [correctly]. (Bug Ticket #14745)


Do you version your environment configuration? Only locally, or with a hosted repository as a backup or to share? Do you run automated tests on it?

5
9
submitted 1 month ago by Kissaki to c/nushell
 
 
  • Simplified Startup Configuration
  • path self
  • chunk-by
  • term query
  • merge deep
  • WASM support (again)
  • sys net inclueds new columns mac and ip
  • raw string pattern matching
  • dates can now be added to durations
  • additional explore keybinds
  • version in startup banner
  • input --default
  • PowerShell script invocation on Windows
  • new introspection tools

Breaking Changes:

  • ++ operator, stricter command signature parsing (resolves silent parse errors)
  • group-by now supports "groupers" (multiple criteria)
  • timeit
  • sys cpu
  • from csv and from tsv
  • std/iter scan
  • completion sorting in custom completers, import module naming with normalization
  • display_output hook
  • du flag changes
  • Code specific environment variables updated during source
6
 
 

Command dl opus to download highest quality audio as or into an opus file through yt-dlp.

Add to config.nu:

# Download highest quality Opus audio as/into .opus file
def "dl opus" [url: string] {
    yt-dlp --extract-audio --audio-quality 0 --audio-format opus $"($url)"
}

Usage:

dl opus <url>

I have set up dl as an alias for yt-dlp via alias dl = yt-dlp. The def above adds an overlaying sub-command.

7
8
7
submitted 3 months ago by Kissaki to c/nushell