Variables and Subexpressions

    The simpler of the two evaluation expressions is the variable. During evaluation, a variable is replaced by its value.

    If we create a variable, we can print its contents by using $ to refer to it:

    Variables in Nushell are immutable, that means that you can not change its value after declaration. They can be shadowed in nested block, that results in:

    1. > let my-value = 4
    2. > do { let my-value = 5; echo $my-value }
    3. > echo $my-value
    4. 4

    A variable path works by reaching inside of the contents of a variable, navigating columns inside of it, to reach a final value. Let’s say instead of 4, we had assigned a table value:

    1. > let my-value = [[name]; [testuser]]

    You can always evaluate a subexpression and use its result by wrapping the expression with parentheses . Note that previous versions of Nushell (prior to 0.32) used $().

    The parentheses contain a pipeline that will run to completion, and the resulting value will then be used. For example, (ls) would run the ls command and give back the resulting table and (git branch --show-current) runs the external git command and returns a string with the name of the current branch. You can also use parentheses to run math expressions like (2 + 3).

    Subexpressions can also be pipelines and not just single commands. If we wanted to get a list of filenames larger than ten kilobytes, we can use an subexpression to run a pipeline and assign this to a variable:

    1. > let names-of-big-files = (ls | where size > 10kb)
    2. > echo $names-of-big-files
    3. ───┬────────────┬──────┬──────────┬──────────────
    4. # │ name │ type │ size │ modified
    5. 0 Cargo.lock File 155.3 KB 17 hours ago
    6. ───┴────────────┴──────┴──────────┴──────────────

    Subexpressions also support paths. For example, let’s say we wanted to get a list of the filenames in the current directory. One way to do this is to use a pipeline:

    1. > ls | get name

    It depends on the needs of the code and your particular style which form works best for you.

    Nushell supports accessing columns in a subexpression using a simple short-hand. You may have already used this functionality before. If, for example, we wanted to only see rows from where the entry is at least ten kilobytes we can write:

    1. > ls | where size > 10kb

    The where size > 10kb is a command with two parts: the command name where and the short-hand expression size > 10kb. We say short-hand because size here is the shortened version of writing $it.size. This could also be written in any of the following ways:

    1. > ls | where $it.size > 10kb
    2. > ls | where ($it.size > 10kb)

    For short-hand syntax to work, the column name must appear on the left-hand side of the operation (like in size > 10kb).