Home Explore Blog CI



nushell

1st chunk of `lang-guide/chapters/strings_and_text.md`
21789517a0d65a4aa402b91598c190d82eb74dd35c47f9df00000001000009aa
# Strings and Text Formatting

## String literals

- Maybe it's the backtick quote?
- Should we have a `r"some\nliteral\tstring"` ala rust?
- Should we have something like python's triple double quotes like `"""` which helps with multi-line strings and also does string literal things?

## String interpolation

String interpolation uses either double quotes or single quotes with a preceding dollar sign. However, when using double quotes, you have to be aware that escapes will be recognized and interpreted.

### Example:

```nu
let name = "Nushell"
print $"My favorite shell is ($name)"
```

There are a couple things to be aware of in the above example.

1. The trigger to recognize a string interpolated string is the `$` sign.
2. Double quotes are used here, but single quotes could be as well. Be aware of escapes when using double quotes.
3. Accessed variable names need to be in parentheses as `$name` is in the example.

### Executing String Interpolated strings

Sometimes you need to build a path to execute external commands or build command arguments.

#### Example:

```nu
let path1 = "/part1"
let path2 = "/part2"
let fn = "filename"
let arguments = ["arg1", "-a", "arg2"]

^$"($path1)($path2)($fn)" ...$arguments
```

The caret `^` before the string interpolation symbol `$` allows that external command to be executed.

## String Quoting

### Double quotes

Double quotes are used as you would normal quotes, except for one thing: escapes can be recognized and interpreted with double quotes.

Example:

```nu
"\e[31mHello\e[35m Nushell\e[0m"
```

This would be interpreted as a red foreground `Hello` and a magenta/purple foreground `Nushell` because:

1. `\e` means insert an `escape` character
2. `[31m` means use whatever is defined as `red` foreground in your terminal
3. `[35m` means use whatever is defined as `magenta/purple` foreground in your terminal.
4. `[0m` means reset all ANSI escape sequences.

There are other escapes defined by Nushell found in [parser.rs](https://github.com/nushell/nushell/blob/main/crates/nu-parser/src/parser.rs#L2496) around line 2500 in the `unescape_string` function.

Recognized Nushell escapes:

- `"` - Double quote
- `'` - Single quote
- `\` - Back slash
- `/` - Forward slash
- `(` - Left parenthesis
- `)` - Right parenthesis
- `{` - Left brace
- `}` - Right brace
- `$` - Dollar sign
- `^` - Caret symbol
- `#` - Hash / pound sign
- `|` - Pipe character
- `~` - Tilde
- `a` - Bel
- `b` - Bs aka Backspace

Title: Strings and Text Formatting in Nushell
Summary
This section covers string literals, string interpolation, and string quoting in Nushell. It discusses the use of backticks, raw strings, and triple double quotes for string literals. String interpolation uses `$`, and variable names must be in parentheses. The `^$` syntax is used to execute string interpolated strings as commands. Double quotes allow escapes, and the document lists the recognized Nushell escapes.