Search

Dark theme | Light theme

May 25, 2025

Nushell Niceties: Filtering Null And Empty Values

Nushell has very useful commands to filter lists and tables. When you have a list with null values, you can use the compact command to filter out the null values. With the option --empty you can also filter out empty items like empty strings, empty lists and empty records. If you want to filter out rows in a table where a column contains a null value, you can use the compact command followed by the name of the column.

In the following example you can see how to use the compact command to filter out null values and empty values from a list:

use std/assert

# You can use compact to filter out null items.
assert equal ([1 2 null 4 5] | compact ) [1 2 4 5]

# With option -e or --empty also empty items like
# "" for strings,
# [] for lists and
# {} for records.
assert equal (["hello" "" "world"] | compact --empty) ["hello" "world"]
assert equal ([[1 2] [] [3 4]] | compact --empty) [[1 2] [3 4]]
assert equal ([{} {name: mrhaki}] | compact -e) [{name: mrhaki}]

The next example shows how to use the compact command with the name of columns to filter out rows in a table where a column contains a null or empty value:

use std/assert

# Create a table with ships and captains.
let ships = [
  [ship captain];
  ["Flying Dutchman"      "Davy Jones"]
  ["Queen Anne's Revenge" "Blackbeard"]
  ["Black Pearl"          null]
  [null                   "Norrington"]
  ["Dauntless"            ""]
  [""                     "Soa Feng"]
]

# Specify column captain in row to filter out null values.
# Rows with null values in other columns are kept.
assert equal ($ships | compact captain) [
  [ship captain];
  ["Flying Dutchman"      "Davy Jones"]
  ["Queen Anne's Revenge" "Blackbeard"]
  [null                   "Norrington"]
  ["Dauntless"            ""]
  [""                     "Soa Feng"]
]

# Specify column ship to filter out null and empty values.
assert equal ($ships | compact --empty ship) [
  [ship captain];
  ["Flying Dutchman"      "Davy Jones"]
  ["Queen Anne's Revenge" "Blackbeard"]
  ["Black Pearl"          null]
  ["Dauntless"            ""]
]

# Multiple colunm names can be used to filter rows with
# null or empty column values.
assert equal ($ships | compact -e ship captain) [
  [ship captain];
  ["Flying Dutchman"      "Davy Jones"]
  ["Queen Anne's Revenge" "Blackbeard"]
]

Written with Nushell 0.104.0.

May 21, 2025

Nushell Niceties: Rolling Dice

Nushell has some nice built-in commands to get randomized data. The random command can be used to get random numbers, strings, and more. You can use the dice subcommand to get random numbers between 1 and 6. The command returns a list of integers. With the option --dice you can specify how many times to throw the dice. By default the dice has 6 sides, but you can use the option --sides to change that. You could roll a dice with 2 sides, like flipping a coin, or roll a dice with 10 sides.

In the following examples you can see how to use the random dice command. With the standard random dice command you see a table with one row. The first column contains the list index and the second column contains the result of the dice throw:

> random dice # throw a standard dice
╭───┬───╮
│ 0 │ 2 │
╰───┴───╯

To get the result of the dice throw you can use the first command to get the value of the second column:

> random dice | first # get the result of the dice throw
3

In the following example you can see how to throw a dice 5 times:

> random dice --dice 5 # throw a standard dice 5 times
╭───┬───╮
│ 0 │ 3 │
│ 1 │ 2 │
│ 2 │ 2 │
│ 3 │ 6 │
│ 4 │ 3 │
╰───┴───╯

To get a dice with only two sides you can use the --sides 2 option:

> random dice --sides 2 # dice with 2 sides, like flipping a coin
╭───┬───╮
│ 0 │ 1 │
╰───┴───╯

In the next examples the options --sides and --dice are combined:

> random dice --sides 2 --dice 5 # dice with 2 sides, like flipping a coin
╭───┬───╮
│ 0 │ 2 │
│ 1 │ 1 │
│ 2 │ 2 │
│ 3 │ 1 │
│ 4 │ 2 │
╰───┴───╯
> random dice --sides 10 --dice 5 # dice with 10 sides
╭───┬───╮
│ 0 │ 3 │
│ 1 │ 4 │
│ 2 │ 6 │
│ 3 │ 3 │
│ 4 │ 8 │
╰───┴───╯

Finally a small example where the result of the dice throw is transformed into a dice icon using the command char --unicode:

> random dice --dice 5 |
	each { char --unicode ($in + 2679 | into string) } # transform into dice icons
╭───┬───╮
│ 0 │ ⚀ │
│ 1 │ ⚅ │
│ 2 │ ⚄ │
│ 3 │ ⚂ │
│ 4 │ ⚁ │
╰───┴───╯

Written with Nushell 0.104.0.

May 10, 2025

Nushell Niceties: Reload Environment

With the command config nu you can change the Nushell configuration. In order to enable any changes you made in the current shell session you must run source $nu.config-path. This will load the configuration in the current shell session. Another option is of course to stop the current shell session and start a new one.

Written with Nushell 0.104.0.

April 26, 2025

Nushell Niceties: Using Request Headers With HTTP Commands

The nice thing about the http command in Nushell is that you can interact with HTTP endpoints without the need to install any external tools. You can use several subcommands like get, post, put, delete and patch. Each of these commands has the options to specify request headers. You can use the option --headers or the short version -H followed by a list of header keys and values.

In the following example you can see how to use the --headers option to specify the request headers:

# Use the --headers option to specify the request headers.
# The headers are specified as a list of header keys and values.
http get --headers [Accept application/json] https://httpbin.org/headers

Instead of specifying the headers in the command you can also create a variable with list of header keys and values:

# Create a variable with a list of header keys and values.
const headers = [
    Accept application/json
    Origin https://mrhaki.com
    Access-Control-Request-Method GET
    Access-Control-Request-Headers content-type
]

# Use the variable with the http get command.
# In this case the short version -H is used.
http get -H $headers https://httpbin.org/headers

Written with Nushell 0.103.0.

April 25, 2025

Nushell Niceties: Posting JSON to an HTTP Endpoint

The http command in Nushell can be used to interact with HTTP endpoints. You can post data to an endpoint using the post subcommand. If you want to post JSON data than you can simply use a record data structure and use the argument --content-type application/json (or the shorthand -t application/json). Nushell will automatically convert the record data structure to JSON and use it as the body of the HTTP request.

In the following example we post a record data structure to the https://httpbin.org/anything endpoint. The record data structure is defined as last argument after the URL:

# You can post JSON data to an endpoint.
# To indicate the content type is JSON
# you can use the --content-type application/json argument.
# After the URL you can specify the data to post
# using a record data structure.
http post --content-type application/json https://httpbin.org/anything {
    name: mrhaki,
    age: 51,
    city: Tilburg
}

The record data structure can be created with variables and commands:

# You can use variables and command in the record data structure.
const username = 'mrhaki'
const firstName = 'Hubert'
const keyLastName = 'lastName'

http post --content-type application/json --full https://httpbin.org/anything {
    name: $username,
    firstName: ($firstName | str downcase),
    $keyLastName: 'Klein Ikkink'
}

Instead of defining the data directly in the http post command you can also create the record data structure first and then pass it to the http post command as last argument:

# The record data structure can be created first and
# then passed to the http post command as last argument.
const data = {
    os: 'MacOS',
    shells: ['Nushell', 'PowerShell']
}

http post --content-type application/json https://httpbin.org/anything $data

You can also use input redirection to pass the record data structure to the http post command. In the following example we pass a record data structure to the http post command using input redirection:

# You can pipe the record data structure to the http post command.
{ username: 'mrhaki' } | http post --content-type application/json https://httpbin.org/anything

Instead of defining the record structure directly as input you can also use the content of a file with JSON data:

# You can read the content of a file with JSON data and
# pass it to the http post command as input using pipes.
{ username: 'mrhaki' } | save data.json
open --raw data.json | http post --content-type application/json https://httpbin.org/anything

As a matter of fact you can use any command that outputs JSON as input to the http post command:

# You can use any command that outputs JSON
# as input to the http post command.
def createJSON [] {
    { username: 'mrhaki' }
}
createJSON | http post --content-type application/json https://httpbin.org/anything

Written with Nushell 0.103.0.

March 21, 2025

Nushell Niceties: Open File With Associated Application

To look at the contents of a file you can use the open command in Nushell. But if we want to open a file with an associated application you must use the start command. For example if you have an HTML file and use the open command you see the HTML source. When you use the start command it opens the HTML file in your default browser.

You can also use the start command to open a directory in the file manager of your operating system. If you use MacOS and type start . it will open the current directory in the Finder application.

When you use the start command followed by a URL it will open the URL in your default browser.

Written with Nushell 0.103.0.

March 18, 2025

SDKMAN! Listing And Upgrading Outdated SDKs And Tools

SDKMAN! is as powerful tool to install and manage software development kits (SDKs) and tools, like Java, Groovy, Gradle, Maven, Spring Boot and Quarkus. If you want to see if a new version of a SDK or tool is available, you can use the sdk upgrade command. This command will list all outdated SDKs and tools. The installed version and the latest version are shown for each SDK and tool. To see if a single SDK or tool is outdated, you can use the name of the SDK or tool as argument to the sdk upgrade command.

In the following example you see the output of a sdk upgrade command for the Spring Boot tool. By answering Y (default answer) to the question Use prescribed default version(s)? you can upgrade the Spring Boot tool to the latest version:

$ sdk upgrade springboot

Available defaults:
springboot (local: 3.1.5, 3.2.4; default: 3.4.3)

Use prescribed default version(s)? (Y/n):

Downloading: springboot 3.4.3

In progress...

####################################################################### 100.0%

Installing: springboot 3.4.3
Done installing!


Setting springboot 3.4.3 as default.
$

You can also run sdk upgrade without an argument and the output shows all SDKs and tools that are outdated. You can upgrade all outdated SDKs and tools by answering Y to the question Use prescribed default version(s)?. In the following example you see the output of a sdk upgrade command without an argument:

$ sdk upgrade

Available defaults:
groovy (local: 4.0.25, 4.0.24, 4.0.23, 3.0.22, 4.0.20; default: 4.0.26)
java (local: 20-tem, 21-tem, 11.0.22-tem, 23-tem, 11.0.25-tem, 21-zulu, 19.0.2-tem, 19.0.1-tem, 17.0.10-tem, 17.0.13-tem, 11.0.8.hs-adpt, 23-graalce, 8.0.432-tem, 21.0.1-tem, 21.0.3-tem, 21.0.2-tem, 21.0.5-tem, 21.0.4-tem; default: 21.0.6-tem)

Use prescribed default version(s)? (Y/n):

Downloading: groovy 4.0.26

In progress...

####################################################################### 100.0%

Installing: groovy 4.0.26
Done installing!


Setting groovy 4.0.26 as default.

Downloading: java 21.0.6-tem

In progress...

####################################################################### 100.0%

Repackaging Java 21.0.6-tem...
Restored java version to 21.0.4-tem (default)

Using java version 11.0.25-tem in this shell.

Done repackaging...
Cleaning up residual files...

Installing: java 21.0.6-tem
Done installing!


Setting java 21.0.6-tem as default.
$

Written with SKDMAN! 5.19.0.

March 14, 2025

Nushell Niceties: Getting The HTTP Response Status

Nushell has a built-in command to invoke HTTP requests: http. You don’t need an external tool like curl or httpie to make HTTP requests. There a lot of options to use with the http command. One of them is the --full or shorter -f option to return a table with extra details of the HTTP request and response. The request and response headers, the body and status are returned in the table. You can easily get information from the table with all the default selection options for a table structure.

In the following example you see the table structure that is returned for a http get request with and without the --full option:

> http get https://www.mrhaki.com/nushell.txt # Without --full you get only the response body.
Nushell rocks!
> http get --full https://www.mrhaki.com/nushell.txt # Use --full to get the full response.
╭─────────┬────────────────────────────────────────────────────────────────────────────────────────────╮
│         │ ╭──────────┬─────────────────────────────────────────────────────────────────────────────╮ │
│ headers │ │ request  │ [list 0 items]                                                              │ │
│         │ │          │ ╭────┬───────────────────────────┬────────────────────────────────────────╮ │ │
│         │ │ response │ │  # │           name            │                 value                  │ │ │
│         │ │          │ ├────┼───────────────────────────┼────────────────────────────────────────┤ │ │
│         │ │          │ │  0 │ age                       │ 51                                     │ │ │
│         │ │          │ │  1 │ cache-status              │ "Netlify Edge"; hit                    │ │ │
│         │ │          │ │  2 │ cache-control             │ public,max-age=0,must-revalidate       │ │ │
│         │ │          │ │  3 │ content-type              │ text/plain; charset=UTF-8              │ │ │
│         │ │          │ │  4 │ content-length            │ 14                                     │ │ │
│         │ │          │ │  5 │ x-nf-request-id           │ 01JP9NJNTBY60HF0TR8X2R3J0K             │ │ │
│         │ │          │ │  6 │ server                    │ Netlify                                │ │ │
│         │ │          │ │  7 │ etag                      │ "010320ca2351da56022222fa858c94ae-ssl" │ │ │
│         │ │          │ │  8 │ accept-ranges             │ bytes                                  │ │ │
│         │ │          │ │  9 │ strict-transport-security │ max-age=31536000                       │ │ │
│         │ │          │ │ 10 │ date                      │ Fri, 14 Mar 2025 06:31:00 GMT          │ │ │
│         │ │          │ ╰────┴───────────────────────────┴────────────────────────────────────────╯ │ │
│         │ ╰──────────┴─────────────────────────────────────────────────────────────────────────────╯ │
│ body    │ Nushell rocks!                                                                             │
│ status  │ 200                                                                                        │
╰─────────┴────────────────────────────────────────────────────────────────────────────────────────────╯
>

To get the response status you can retrieve the value from the status field that is in the table:

# With the --full option a table is returned by http get with the fields
# headers, body and status.

# Using get to get the value of the status field.
assert equal (http get --full https://www.mrhaki.com/nushell.txt | get status) 200

# Using select to get a record with the result.
assert equal (http get -f https://www.mrhaki.com/nushell.txt | select status) {status: 200}

Written with Nushell 0.102.0.

March 11, 2025

Nushell Niceties: Trimming Strings

Nushell has some powerful commands to work with strings. The str trim command can be used to trim whitespace from a string. By default whitespace from the beginning and end of a string is removed. To only remove whitespace from the beginning of the string you can use the --left argument (or the shorter option -l). For removing whitespace from the end of the string you can use the option --right or the shorter option -r. To trim a string based on another character you can use the option --char or the shorter option -c followed by the character to trim.

In the following example you can see how the str trim command can be used to remove whitespace:

use std/assert

# str trim removes whitespace from the beginning and end of the string
assert equal (' mrhaki ' | str trim) 'mrhaki'

# Use --left or -l to only remove whitespace from the left.
assert equal (' mrhaki ' | str trim --left) 'mrhaki '

# Use --right or -r to only remove whitespace from the right.
assert equal (' mrhaki ' | str trim --right) ' mrhaki'

# With double quoted strings sr trim removes not
# only spaces, but also other characters like \t and \n.
assert equal ("\tmrhaki\n" | str trim) 'mrhaki'
assert equal ("\t mrhaki \n" | str trim) 'mrhaki'

You can use the argument --char or -c to specify another character to use for trimming:

use std/assert

# The str trim command with the --char or -c argument
# to specify a character to trim.
assert equal ('**mrhaki**' | str trim --char '*') 'mrhaki'
assert equal ('* mrhaki *' | str trim -c '*') ' mrhaki '

You can specify the name of keys of a record to trim the values of those keys:

use std/assert

# The str trim command accepts names of keys to
# trim the values of those keys in a record.
let user = {
    name: 'Hubert Klein Ikkink ',
    alias: ' mrhaki '
} | str trim name alias

assert equal $user {
    name: 'Hubert Klein Ikkink',
    alias: 'mrhaki'
}

The str trim command can be applied to lists to trim each element as you can see in the next example:

use std/assert

# The str trim command can be applied to lists
# to trim each element.
let shells = [' NuShell', 'Powershell '] | str trim
assert equal $shells ['NuShell', 'Powershell']

Finally the str trim command can be used to trim the values in one or more columns in a table:

use std/assert

# The str trim command also accepts names of columns for a table.
# The values in the column are trimmed.
let users = [
    [name alias];
    ['Hubert Klein Ikkink ' 'mrhaki ']
] | str trim --right alias

# Name column is not transformed.
assert equal $users.0.name 'Hubert Klein Ikkink '
# Alias column is transformed.
assert equal $users.0.alias 'mrhaki'

Written with Nushell 0.102.0.

March 2, 2025

Nushell Niceties: Getting The Current Version Of Nushell

The version command can be used to get the current version of Nushell. The result is a record structure with different keys with information about the Nushell version. For example the property version contains the current version of Nushell, the properties major, minor and patch contain the major, minor and patch version of Nushell.

The version command accepts the argument check to check if a newer version of Nushell is available. The returned record structure contains the property current with a value true or false depending on whether a newer version is available. The version check command also return the latest version as value of the property latest.

In the following example you see output of the version command:

> version
╭────────────────────┬────────────────────────────────────────────────╮
│ version            │ 0.102.0                                        │
│ major              │ 0                                              │
│ minor              │ 102                                            │
│ patch              │ 0                                              │
│ branch             │                                                │
│ commit_hash        │                                                │
│ build_os           │ macos-x86_64                                   │
│ build_target       │ x86_64-apple-darwin                            │
│ rust_version       │ rustc 1.84.1 (e71f9a9a9 2025-01-27) (Homebrew) │
│ cargo_version      │ cargo 1.84.1                                   │
│ build_time         │ 2025-02-04 15:49:35 +00:00                     │
│ build_rust_channel │ release                                        │
│ allocator          │ mimalloc                                       │
│ features           │ default, sqlite, trash                         │
│ installed_plugins  │                                                │
╰────────────────────┴────────────────────────────────────────────────╯

You can also use all record navigation options to get specific information from the version command output:

> version | get version
0.102.0
> version | get features
default, sqlist, trash

The following example output of the version check command shows the latest version of Nushell is used:

> version check
╭─────────┬─────────╮
│ channel │ release │
│ current │ true    │
│ latest  │ 0.102.0 │
╰─────────┴─────────╯

The following example uses the if command to check if the current version of Nushell is the latest version:

# Check if the current property of version check is true.
> if (version check).current {
    let v = version
    print $'You are using the latest version of Nushell -> ($v.version)'
}
You are using the latest version of Nushell -> 0.102.0

Written with Nushell 0.102.0.