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
Nushell has many other similarities with Bash (and other shells) and many commands in common.
23
23
24
24
::: tip
25
-
While the above commandline works, in Nushell there's just no need to use the `curl` and `jq` commands for this, since Nushell has a built-in [`http get` command](/commands/docs/http_get.md) and handles JSON data natively. For example:
25
+
Bash is primarily a command interpreter which runs external commands. Nushell provides many of these as cross-platform, built-in commands.
26
+
27
+
While the above commandline works in both shells, in Nushell there's just no need to use the `curl` and `jq` commands. Instead, Nushell has a built-in [`http get` command](/commands/docs/http_get.md) and handles JSON data natively. For example:
26
28
27
29
```nu
28
30
http get https://api.github.com/repos/nushell/nushell/contributors | select login contributions
@@ -209,12 +211,20 @@ to combine simple commands together to achieve complex results.
209
211
210
212
## Think of Nushell as a Compiled Language
211
213
212
-
In Nushell, there are exactly two, high-level stages when running code:
214
+
In Nushell, there are exactly two, separate, high-level stages when running code:
215
+
216
+
1._Stage 1 (Parser):_ Parse the **_entire_** source code
217
+
2._Stage 2 (Engine):_ Evaluate the **_entire_** source code
218
+
219
+
It can be useful to think of Nushell's parsing stage as _compilation_ in [static](./how_nushell_code_gets_run.md#dynamic-vs-static-languages) languages like Rust or C++. By this, we mean that all of the code that will be evaluated in Stage 2 must be **_known and available_** during the parsing stage.
220
+
221
+
::: important
222
+
However, this also means that Nushell cannot currently support an `eval` construct as with _dynamic_ languages such as Bash or Python.
223
+
:::
213
224
214
-
1._Stage 1 (Parser):_ Parse the entire source code
215
-
2._Stage 2 (Engine):_ Evaluate the entire source code
225
+
### Features Built on Static Parsing
216
226
217
-
The Nushell Parser is key to many features of Nushell and its REPL, such as:
227
+
On the other hand, the **_static_** results of Parsing are key to many features of Nushell its REPL, such as:
218
228
219
229
- Accurate and expressive error messages
220
230
- Semantic analysis for earlier and robust detection of error conditions
@@ -229,13 +239,9 @@ The Nushell Parser is key to many features of Nushell and its REPL, such as:
229
239
- (Future) Formatting
230
240
- (Future) Saving IR (Intermediate Representation) "compiled" results for faster execution
231
241
232
-
It can be useful to think of Nushell's parsing stage as _compilation_ in [static](./how_nushell_code_gets_run.md#dynamic-vs-static-languages) languages like Rust or C++. By this, we mean that all of the code that will be evaluated in Stage 2 must be **_known and available_** during the parsing stage.
242
+
### Limitations
233
243
234
-
::: important
235
-
However, this also means that Nushell cannot currently support an `eval` construct as with _dynamic_ languages such as Bash or Python.
236
-
:::
237
-
238
-
This often leads to confusion for users coming to Nushell from languages where an `eval` is available.
244
+
The static nature of Nushell often leads to confusion for users coming to Nushell from languages where an `eval` is available.
239
245
240
246
Consider a simple two-line file:
241
247
@@ -258,7 +264,7 @@ The following examples use the [`source` command](/commands/docs/source.md), but
258
264
259
265
:::
260
266
261
-
### Example: Dynamically Generating Source
267
+
####Example: Dynamically Generating Source
262
268
263
269
Consider this scenario:
264
270
@@ -290,7 +296,7 @@ The limitation only occurs when both are parsed _together_ as a single expressio
290
296
See the [REPL](./how_nushell_code_gets_run.md#the-nushell-repl) section in _"How Nushell Code Gets Run"_ for more explanation.
291
297
:::
292
298
293
-
### Example: Dynamically Creating a Filename to be Sourced
299
+
####Example: Dynamically Creating a Filename to be Sourced
294
300
295
301
Another common scenario when coming from another shell might be attempting to dynamically create a filename that will be sourced:
See [Parse-time Constant Evaluation](./how_nushell_code_gets_run.md#parse-time-constant-evaluation) for more details.
352
358
:::
353
359
354
-
### Example: Change to a different directory (`cd`) and `source` a file
360
+
####Example: Change to a different directory (`cd`) and `source` a file
355
361
356
362
Here's one more — Change to a different directory and then attempt to `source` a file in that directory.
357
363
@@ -388,7 +394,7 @@ Nushell is designed to use a single Parsing stage for each expression or file. T
388
394
389
395
## Variables are Immutable by Default
390
396
391
-
Another common surprise when coming from other languages is that Nushell variables are immutable by default. Coming to Nushell, you'll want to spend some time becoming familiar with working in a more functionalstyle, as this tends to help write code that works best with immutable variables.
397
+
Another common surprise when coming from other languages is that Nushell variables are immutable by default. While Nushell has optional mutable variables, many of Nushell's commands are based on a functional-style of programming which requires immutability.
392
398
393
399
Immutable variables are also key to Nushell's [`par-each` command](/commands/docs/par-each.md), which allows you to operate on multiple values in parallel using threads.
394
400
@@ -417,7 +423,9 @@ ls | each { |row|
417
423
418
424
The [`cd`](/commands/docs/cd.md) command changes the `PWD` environment variables, but this variable change does not survive past the end of the block. This allows each iteration to start from the current directory and then enter the next subdirectory.
419
425
420
-
Having a scoped environment makes commands more predictable, easier to read, and when the time comes, easier to debug. Nushell also provides helper commands like [`load-env`](/commands/docs/load-env.md) as a convenient way of loading multiple updates to the environment at once.
426
+
Having a scoped environment makes commands more predictable, easier to read, and when the time comes, easier to debug. It's also another feature that is key to the `par-each` command we discussed above.
427
+
428
+
Nushell also provides helper commands like [`load-env`](/commands/docs/load-env.md) as a convenient way of loading multiple updates to the environment at once.
0 commit comments