owned this note changed 3 years ago
Published Linked with GitHub

Quoting

What's a better paradigm for quoting in nushell?
This was written based on development version 0.69.2 of nushell Oct 14, 2022.

Current

Double quotes

Double quotes are used as you would think normal quotes should be used except for one thing. That one thing is escapes can be recognized and interpreted with double quotes.

Example:

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

That would be interpreted with Red foreground Hello and Purple aka Magenta foreground Nushell becuase:

  1. \e means insert and escape character
  2. [31m means use whatever is defined as red foreground in your terminal
  3. [35m means use whatever is defined as purple, sometimes called magenta, foreground in your terminal.
  4. [0m means reset all ansi escape sequences.

There are other escapes that are defined by nushell found in parser.rs around line 2426 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
  • e - escape
  • f - form feed
  • n - line feed aka new line
  • r - carriage return
  • t - tab aka horizontal tab
  • uXXXX - unicode hex value for a char - requires 4 chars. It would be nice if \uXX was acceptible as well.

Double quotes work within string interpolation as well.

Single qotes

The single quote character should work identicaly to the double quote except that escape characters will not be recognized and interpreted.

Single quotes work within string interpolation as well.

Backtick quotes

Backtick quotes are something I'm still fuzzy on. Originally I thought they were supposed to be used as our string literal representation of quotes. Maybe that's what it is now. I'm not sure to tell.

Here are some ways we see/use backtick quotes.

  1. If you're using tab to complete directories with spaces, backtick quotes will be used to wrap the string. The only issue with this is that when you want to complete the next folder after one with a space, you have to move the cursor backward inside the last backtick quote before you hit tab again to complete the next level or file.
  2. Backtick quotes do not work in string interpolation. Should they?
  3. I believe backtick quotes can be used the same way as double quotes and single quotes but they do not recognize and interpret escapes.
  4. Another definition from Kubouch is backtick quotes are supposed to be like bare words that support spaces. As an example JT just landed a PR that allows backtick quotes to autocd. So, in Windows, if you're at C:\ you could type `Program Files` and it would change to that directory.

String literal

Maybe it's the backtick quote?

String interpolation

String interpolation uses either double quotes or single quotes with a preceeding dollar sign. However, when using double quotes, you have to be aware that escapes will be recognized and interpreted. (I(darren) really don't like that people have to be aware of this functionality with double quotes.)

Example:

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 use here but single quotes could be used as well. User be aware of escapes if using double quotes.
  3. Access variable names needs to be parenthesis as $name is in the example.

Executing String Interpolated strings

Sometimes you need to build a path to execute external commands.

Example:

let path1 = "/part1"
let path2 = "/part2"
let fn = "filename"

^$"($path1)($path2)($fn)"

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

Nested Quotes

Sometimes you need to nest quotes. I think this could use some work because sometimes I start with single quotes on the outside and have to reverse course to use double quotes on the outside. I'm not sure if backtick quotes can participate here.

Example:

"This is just a string 'that needs an inner part quoted'"
'This is also a string "that needs an inner part quoted"'

The key to always remember is that double quotes recognize and interpret escapes so if you have any \ characters in your string, they will be interpreted as excapes. The following is an example of a question we get frequently on Discord.

Why doesn't this work?
> cd "C:\Program Files\somedir"

It doesn't work because it sees \P and \s as escapes that are not recognized.

Future Ideas

These are some future ideas meant to make string quoting more intuitive. Feel free to jump in and add ideas.

  • Should we use """ ala python for string literals. I was corrected that triple double quotes mean multiline strings in python and string literal.
  • Maybe remove escapes from double quotes since double quotes are the standard way to quote things and perhaps make single quotes recognize and interpret escape characters.
  • I wonder whether we should get rid of that difference and make single- and double- quotes behave essentially the same (as in e.g. Python). The special behavior of escapes like \n above could be triggered by a string prefix such as r"a\nb", which is a syntactical features that again Python has, and also Scala, and maybe others? And nushell already has precedent for using string prefixes to configure the behavior of the string construct (i.e. $"this string will do interpolation in parens") (from dan davison on Discord)
Select a repo