Working with strings
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:
Single-quoted strings don’t do anything to the text they’re given, making them ideal for holding a wide range of text data.
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:
Escape characters let you quickly add in a character that would otherwise be hard to type.
\"
- double-quote character\'
- single-quote character\\
- backslash\/
- forward slash- - formfeed
\r
- carriage return\n
- newline (line feed)\t
- tab
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:
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.
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 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.
Many string functions are subcommands of the str
command. You can get a full list using .
For example, you can look if a string contains a particular character using str contains
: