Scripting with Nushell

Published Sat Jan 27 2024

As a command-line nerd I am ashamed to admit I have avoided writing my own scripts for a long time. Scripting language syntax has been a big barrier to me, often unstructured compared with programming languages I am accustomed to.

Enter Nushell, a modern shell that has a greatly improved scripting experience. Here’s a couple things I have enjoyed about it.

Structured Data

Nushell goes a step past most shells, parsing command output into structured data instead of formatted strings. This leads to some huge benefits, especially in scripts. Here is an example of Nushell’s output for ls compared with my Fish shell ls:

Fish shell: ls with fish

Now with Nushell: ls with nushell

Nushell’s output for the same directory is very different. This table structure can be used to iterate over file objects. Having shell APIs return structured data makes writing simple scripts easier.

Error Messages

Nushell is written in Rust (btw). One of the big upsides of this is Rust-style error messages for your shell! Today I wrote a quick script to run my custom font through the NerdFont Patcher. While working on my script I forgot to prefix a variable with $, resulting in this helpful error:

nushell error

The message could be better here, suggesting I add my missing symbol $ That being said, having any error message at all is a huge step up from the shell experience I am used to. These errors appear before your script is run. When working on a script I have less failed trial-runs because small issues with my code are presented as errors rather than mysterious bugs after running.