recursively search for files by file name:
```nu
glob **/*.{rs,toml} --depth 2
```
watch a file, run command whenever it changes:
```nu
watch . --glob=**/*.rs {|| cargo test }
```
## Custom Commands
custom command with parameter type set to string:
```nu
def greet [name: string] {
$"hello ($name)"
}
```
custom command with default parameter set to nushell:
```nu
def greet [name = "nushell"] {
$"hello ($name)"
}
```
passing named parameter by defining flag for custom commands:
```nu
def greet [
name: string
--age: int
] {
[$name $age]
}
greet world --age 10
```
using flag as a switch with a shorthand flag (-a) for the age:
```nu
def greet [
name: string
--age (-a): int
--twice
] {
if $twice {
[$name $age $name $age]
} else {
[$name $age]
}
}
greet -a 10 --twice hello
```
custom command which takes any number of positional arguments using rest params:
```nu
def greet [...name: string] {
print "hello all:"
for $n in $name {
print $n
}
}
greet earth mars jupiter venus
# => hello all:
# => earth
# => mars
# => jupiter
# => venus
```
## Variables
an immutable variable cannot change its value after declaration:
```nu
let val = 42
print $val
# => 42
```
shadowing variable (declaring variable with the same name in a different scope):
```nu
let val = 42
do { let val = 101; $val }
# => 101
$val
# => 42
```
declaring a mutable variable with mut key word:
```nu
mut val = 42
$val += 27
$val
# => 69
```
closures and nested defs cannot capture mutable variables from their environment (errors):
```nu
mut x = 0
[1 2 3] | each { $x += 1 }
# => Error: nu::parser::expected_keyword
# =>
# => × Capture of mutable variable.
# => ╭─[entry #83:1:18]
# => 1 │ [1 2 3] | each { $x += 1 }
# => · ─┬
# => · ╰── capture of mutable variable
# => ╰────
```
a constant variable is immutable and is fully evaluated at parse-time:
```nu
const file = 'path/to/file.nu'
source $file
```
use question mark operator `?` to return null instead of error if provided path is incorrect:
```nu
let files = (ls)
$files.name?.0?
```
assign the result of a pipeline to a variable:
```nu
let big_files = (ls | where size > 10kb)
$big_files
```
## Modules
use an inline module:
```nu
module greetings {
export def hello [name: string] {
$"hello ($name)!"
}
export def hi [where: string] {
$"hi ($where)!"
}
}
use greetings hello
hello "world"
```
import module from file and use its environment in current scope:
```nu
# greetings.nu
export-env {
$env.MYNAME = "Arthur, King of the Britons"
}
export def hello [] {
$"hello ($env.MYNAME)"
}
use greetings.nu
$env.MYNAME
# => Arthur, King of the Britons
greetings hello
# => hello Arthur, King of the Britons!
```
use main command in module:
```nu
# greetings.nu
export def hello [name: string] {
$"hello ($name)!"
}
export def hi [where: string] {
$"hi ($where)!"
}
export def main [] {
"greetings and salutations!"
}
use greetings.nu
greetings
# => greetings and salutations!
greetings hello world
# => hello world!
```