You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lang-guide/lang-guide.md
+53-49Lines changed: 53 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,7 +31,7 @@ Integer numbers.
31
31
32
32
Internally represented as a signed 64-bit number with two's complement arithmetic.
33
33
34
-
Numeric literals without a fractional components will evaluate as `Int`: `1`, `-2`, `1000`
34
+
Numeric literals without a fractional component will evaluate as `Int`: `1`, `-2`, `1000`
35
35
36
36
Integers can be created using hex `0xff`, octal `0o234`, binary `0b10101`, and decimal `123`.
37
37
@@ -116,9 +116,9 @@ As strings have to be valid UTF-8 for effective string operations, they can not
116
116
117
117
Different display operations might impose limitations on which non-printable or printable characters get shown. One relevant area are the ANSI escape sequences that can be used to affect the display on the terminal. Certain operations may choose to ignore those.
118
118
119
-
To input string data different [string literals](#string-literals) supporting escaping and [string interpolation](#string-interpolation) are available.
119
+
To input string data, different [string literals](#string-literals) supporting escaping and [string interpolation](#string-interpolation) are available.
120
120
121
-
TBD: On which level string indexing should be performed: bytes or unicode scalars.
121
+
TBD: On which level string indexing should be performed: bytes or Unicode scalars.
122
122
123
123
### Record
124
124
@@ -184,7 +184,7 @@ Nushell offers a way of creating binary literals in your data. These are in one
The data inside of the `[]` represents a single data value of bits.
190
190
@@ -249,7 +249,7 @@ Nushell provides support for these bitwise operators:
249
249
250
250
### if
251
251
252
-
The`if` expression evaluates a condition and then chooses to run a block based on the condition.
252
+
The`if` expression evaluates a condition and then chooses to run a block based on the condition.
253
253
254
254
For example, you can print "yes", based on a true condition:
255
255
@@ -315,20 +315,20 @@ The code that follows the `else` is an expression rather than a block, allowing
315
315
316
316
## String interpolation
317
317
318
-
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.)
318
+
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. (I(darren) really don't like that people have to be aware of this functionality with double quotes.)
319
319
320
320
Example:
321
321
322
322
```nu
323
-
let name = "nushell"
323
+
let name = "Nushell"
324
324
print $"My favorite shell is ($name)"
325
325
```
326
326
327
327
There are a couple things to be aware of in the above example.
328
328
329
329
1. The trigger to recognize a string interpolated string is the `$` sign.
330
-
2. Double quotes are use here but single quotes could be used as well. User be aware of escapes if using double quotes.
331
-
3.Access variable names needs to be parenthesis as `$name` is in the example.
330
+
2. Double quotes are used here, but single quotes could be as well. Be aware of escapes when using double quotes.
331
+
3.Accessed variable names need to be in parentheses as `$name` is in the example.
332
332
333
333
### Executing String Interpolated strings
334
334
@@ -344,58 +344,58 @@ let fn = "filename"
344
344
^$"($path1)($path2)($fn)"
345
345
```
346
346
347
-
The caret `^` before the string interpolation symbol `$` allows that external command to be exectued.
347
+
The caret `^` before the string interpolation symbol `$` allows that external command to be executed.
348
348
349
349
## String Quoting
350
350
351
351
### Double quotes
352
352
353
-
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.
353
+
Double quotes are used as you would normal quotes, except for one thing: escapes can be recognized and interpreted with double quotes.
354
354
355
355
Example:
356
356
357
357
```nu
358
358
"\e[31mHello\e[35m Nushell\e[0m"
359
359
```
360
360
361
-
That would be interpreted with `Red`foreground Hello and `Purple aka Magenta`foreground Nushell becuase:
361
+
This would be interpreted as a red foreground `Hello` and a magenta/purple foreground `Nushell` because:
362
362
363
-
1.`\e` means insert and`escape` character
363
+
1.`\e` means insert an`escape` character
364
364
2.`[31m` means use whatever is defined as `red` foreground in your terminal
365
-
3.`[35m` means use whatever is defined as purple, sometimes called magenta, foreground in your terminal.
366
-
4.`[0m` means reset all ansi escape sequences.
367
-
368
-
There are other escapes that are defined by nushell found in parser.rs around line 2426 in the `unescape_string` function.
369
-
370
-
Recognized nushell escapes:
371
-
372
-
- " - double quote
373
-
- ' - single quote
374
-
- \ - back slash
375
-
- / - forward slash
376
-
- ( - left parenthesis
377
-
- ) - right parenthesis
378
-
- { - left brace
379
-
- } - right brace
380
-
- $ - dollar sign
381
-
- ^ - caret symbol
382
-
-\# - hash / pound sign
383
-
-\| - pipe character
384
-
- ~ - tilde
385
-
- a - bel
386
-
- b - bs aka backspace
387
-
- e - escape
388
-
- f - form feed
389
-
- n - line feed aka new line
390
-
- r - carriage return
391
-
- t - tab aka horizontal tab
392
-
- uXXXX - unicode hex value for a char - requires 4 chars. It would be nice if \uXX was acceptible as well.
365
+
3.`[35m` means use whatever is defined as `magenta/purple` foreground in your terminal.
366
+
4.`[0m` means reset all ANSI escape sequences.
367
+
368
+
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.
369
+
370
+
Recognized Nushell escapes:
371
+
372
+
- " - Double quote
373
+
- ' - Single quote
374
+
- \ - Back slash
375
+
- / - Forward slash
376
+
- ( - Left parenthesis
377
+
- ) - Right parenthesis
378
+
- { - Left brace
379
+
- } - Right brace
380
+
- $ - Dollar sign
381
+
- ^ - Caret symbol
382
+
-\# - Hash / pound sign
383
+
-\| - Pipe character
384
+
- ~ - Tilde
385
+
- a - Bel
386
+
- b - Bs aka Backspace
387
+
- e - Escape
388
+
- f - Form feed
389
+
- n - Line feed aka New Line
390
+
- r - Carriage return
391
+
- t - Tab aka Horizontal Tab
392
+
- uXXXX - Unicode hex value for a char - requires 4 chars. It would be nice if \uXX was acceptable as well.
393
393
394
394
Double quotes work within string interpolation as well.
395
395
396
396
### Single quotes
397
397
398
-
The single quote character should work identicaly to the double quote _except_ that escape characters will not be recognized and interpreted.
398
+
The single quote character should work identically to the double quote _except_ that escape characters will not be recognized and interpreted.
399
399
400
400
Single quotes work within string interpolation as well.
401
401
@@ -405,7 +405,7 @@ Backtick quotes are something I'm still fuzzy on. Originally I thought they were
405
405
406
406
Here are some ways we see/use backtick quotes.
407
407
408
-
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.
408
+
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.
409
409
2. Backtick quotes do not work in string interpolation. Should they?
410
410
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.
411
411
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.
@@ -421,7 +421,7 @@ Example:
421
421
'This is also a string "that needs an inner part quoted"'
422
422
```
423
423
424
-
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.
424
+
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 escapes. The following is an example of a question we get frequently on Discord.
425
425
426
426
```nu
427
427
Why doesn't this work?
@@ -454,18 +454,19 @@ It doesn't work because it sees `\P` and `\s` as escapes that are not recognized
454
454
455
455
### Best practices for pipeline commands
456
456
457
-
### Interaction with unix pipes
457
+
### Interaction with Unix pipes
458
458
459
459
### Handling stdout and stderr
460
460
461
461
You can handle stderr in multiple ways:
462
-
1. do nothing, stderr will be printed directly
463
-
2. pipe stderr to the next command, using `e>|` or `e+o>|`
464
-
3. redirect stderr to a file, using `e> file_path`, or `e+o> file_path`
465
-
4. use `do -i { cmd } | complete` to capture both stdout and stderr as structured data
466
462
463
+
1. Do nothing, stderr will be printed directly
464
+
2. Pipe stderr to the next command, using `e>|` or `e+o>|`
465
+
3. Redirect stderr to a file, using `e> file_path`, or `e+o> file_path`
466
+
4. Use `do -i { cmd } | complete` to capture both stdout and stderr as structured data
467
467
468
468
For the next examples, let's assume this file:
469
+
469
470
```nushell
470
471
# demo.nu
471
472
print "foo"
@@ -497,6 +498,7 @@ Redirection to a file:
497
498
| use `complete` | `let result = do { nu demo.nu } \| complete` | record containing both stdout and stderr
498
499
499
500
Note that `e>|` and `o+e>|` only work with external command, if you pipe internal commands' output through `e>|` and `o+e>|`, you will get an error:
501
+
500
502
```
501
503
❯ ls e>| str length
502
504
Error: × `e>|` only works with external streams
@@ -516,12 +518,14 @@ Error: × `o+e>|` only works with external streams
516
518
```
517
519
518
520
You can also redirect `stdout` to a file, and pipe `stderr` to next command:
521
+
519
522
```
520
523
nu demo.nu o> file.txt e>| str upcase
521
524
nu demo.nu e> file.txt | str upcase
522
525
```
523
526
524
527
But you can't use redirection along with `o+e>|`, because it's ambiguous:
0 commit comments