Home Explore Blog CI



nushell

2nd chunk of `blog/2021-01-05-nushell_0_25.md`
55f1883664f362c310a237490f66631bc887f9c39a114607000000010000084d
    = $x + $y
}

add "bob" 4
```

Now if you run the example, you'll see a type error like this:

```
error: Type Error
  ┌─ shell:5:5
  │
5 │ add "bob" 4
  │     ^^^^^ Expected int, found "bob"
```

Here's a list of the types that are allowed:

- int - an integer
- string - a string
- path - a filepath
- table - a table
- unit - a number with a unit (like `10kb`)
- number - an integer or decimal number
- pattern - a glob pattern (like `foo*`)
- range - a numeric range (like `1..10`)
- block - a code block (like `{ ls }`)
- any - any of the above types (this is assumed if you leave off the type)

_Note: Nushell is whitespace-significant, so the variable + ':' + type need to be united as one, without spaces._

You can also create flags for your commands:

```
def create-item(name, --age:int) { ... }

create-item "driver" --age 20
```

## Variables

You can now also define variables using `let`.

```
let name = "Nushell"
echo $name
```

These variables are created in the scope they're defined in.

If, for example, we had written this instead:

```
do {
    let $name = "Nu"
    echo $name   # prints "Nu"
}
echo $name       # this will fail
```

Once we leave the block above, the `name` variable is no longer visible.

These variables are **immutable** and need to be initialized as they are defined.

Along with variables, we also support "shadowing", so that you can create a variable inside of a scope, and have it "shadow" the variable of the same name outside of the scope:

```
let size = 10
do {
    let size = 5
    echo $size     # prints 5
}
echo $size         # prints 10
```

## Environment variables

You can also use `let-env` to create environment variables. Just like variables, they are created in the scope they're defined.

```
let-env TRACE = on
echo $nu.env.TRACE   # prints 'on'
```

## Aliases

With 0.25, we've also changed how aliases work to be more like a text expansion, in the spirit of how aliases work in shells like Bash.

```
alias ll = ls -l
ll -a
```

This lets you alias a larger command to a smaller name, and then also pass additional arguments and flags to it.

Title: Nushell 0.25: Type Annotations, Variable Scoping, and Aliases
Summary
Nushell 0.25 expands on custom commands, demonstrating how type annotations prevent incorrect argument types. It introduces variable scoping with `let`, including immutability and shadowing. Environment variables can be created with `let-env`. Aliases now function as text expansions, allowing additional arguments to be passed.