Working with strings

Strings in Nushell help to hold text data for later use. This can include file names, file paths, names of columns, and much more. Strings are so common that Nushell offers a couple ways to work with them, letting you pick what best matches your needs.

Single-quoted string

The simplest string in Nushell is the single-quoted string. This string uses the ' character to surround some text. Here's the text for hello world as a single-quoted string:

> 'hello world'
hello world

Single-quoted strings don't do anything to the text they're given, making them ideal for holding a wide range of text data.

Double-quoted strings

For more complex strings, Nushell also offers double-quoted strings. These strings use the " character to surround text. They also support the ability escape characters inside the text using the \ character.

For example, we could write the text hello followed by a new line and then world, using escape characters and a double-quoted string:

> "hello\nworld"

Escape characters let you quickly add in a character that would otherwise be hard to type.

Nushell currently supports the following escape characters:

  • \" - double-quote character
  • \' - single-quote character
  • \\ - backslash
  • \/ - forward slash
  • \b - backspace
  • \f - formfeed
  • \r - carriage return
  • \n - newline (line feed)
  • \t - tab
  • \uXXXX - a unicode character (replace XXXX with the number of the unicode character)

String interpolation

More complex string use cases also need a new form of string: string interpolation. This is a way of building text from both raw text and the result of running expressions. String interpolation combines the results together, giving you a new string.

String interpolation uses $" " and $' ' as ways to wrap interpolated text.

For example, let's say we have a variable called $name and we want to greet the name of the person contained in this variable:

> let name = "Alice"
> $"greetings, ($name)"
greetings, Alice

By wrapping expressions in (), we can run them to completion and use the results to help build the string.

String interpolation has both a single-quoted, $' ', and a double-quoted, $" ", form. These correspond to the single-quoted and double-quoted strings: single-quoted string interpolation doesn't support escape characters while double-quoted string interpolation does.

As of version 0.61, interpolated strings support escaping parentheses, so that the ( and ) characters may be used in a string without Nushell trying to evaluate what appears between them:

> $"2 + 2 is (2 + 2) \(you guessed it!)"
2 + 2 is 4 (you guessed it!)

Splitting strings

The split row command creates a list from a string based on a delimiter. For example, let colors = ("red,green,blue" | split row ",") creates the list [red green blue].

The split column command will create a table from a string based on a delimiter. For example let colors = ("red,green,blue" | split column ",") creates a table, giving only column to each element.

Finally, the split chars command will split a string into a list of characters.

The str command

Many string functions are subcommands of the str command. You can get a full list using help str.

For example, you can look if a string contains a particular character using str contains:

> "hello world" | str contains "w"

Trimming Strings

You can trim the sides of a string with the str trim command. By default, the str trim commands trims whitespace from both sides of the string. For example

> '       My   string   ' | str trim
My   string

You can specificy on which side the trimming occurs with the --right and --left options.

To trim a specific character, use --char <Character> to specificy the character to trim.

Here's an example of all the options in action:

> '=== Nu shell ===' | str trim -r -c '='
=== Nu shell


Substrings are slices of a string. They have startpoint and an endpoint. Here's an example of using a substring:

> 'Hello World!' | str index-of 'o'
> 'Hello World!' | str index-of 'r'
> 'Hello World!' | str substring '4,8'
o Wo

String padding

With the str lpad and str rpad commands you can add padding to a string. Padding adds characters to string until it's a certain length. For example:

> '1234' | str lpad -l 10 -c '0'
> '1234' | str rpad -l 10 -c '0' | str length

Reversing Strings

This can be done easily with the str reverse command.

> 'Nushell' | str reverse
> ['Nushell' 'is' 'cool'] | str reverse
│ 0 │ llehsuN │
│ 1 │ si      │
│ 2 │ looc    │

String Parsing

With the parse command you can parse a string into columns. For example:

> 'Nushell is the best' | parse '{shell} is {type}'
│ # │  shell  │   type   │
│ 0 │ Nushell │ the best │
> 'Bash is kinda cringe' | parse --regex '(?P<shell>\w+) is (?P<type>[\w\s]+)'
│ # │ shell │     type     │
│ 0 │ Bash  │ kinda cringe │

Converting Strings

There are multiple ways to convert strings to and from other types.

To String

  1. Using into string. e.g. 123 | into string
  2. Using string interpolation. e.g. $'(123)'
  3. Using build-string. e.g. build-string (123)

From String

  1. Using into <type>. e.g. '123' | into int

Coloring Strings

You can color strings with the ansi command. For example:

> $'(ansi purple_bold)This text is a bold purple!(ansi reset)'

ansi purple_bold makes the text a bold purple ansi reset resets the coloring to the default. (Tip: You should always end colored strings with ansi reset)