Home Explore Blog CI



nushell

commands/docs/def.md
3eb6ef98f54c991550548a1718a1875b14cbbb596b3baa5500000003000006e4
---
title: def
categories: |
  core
version: 0.104.0
core: |
  Define a custom command.
usage: |
  Define a custom command.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

# `def` for [core](/commands/categories/core.md)

<div class='command-title'>Define a custom command.</div>

## Signature

```> def {flags} (def_name) (params) (block)```

## Flags

 -  `--env`: keep the environment defined inside the command
 -  `--wrapped`: treat unknown flags and arguments as strings (requires ...rest-like parameter in signature)

## Parameters

 -  `def_name`: Command name.
 -  `params`: Parameters.
 -  `block`: Body of the definition.


## Input/output types:

| input   | output  |
| ------- | ------- |
| nothing | nothing |
## Examples

Define a command and run it
```nu
> def say-hi [] { echo 'hi' }; say-hi
hi
```

Define a command and run it with parameter(s)
```nu
> def say-sth [sth: string] { echo $sth }; say-sth hi
hi
```

Set environment variable by call a custom command
```nu
> def --env foo [] { $env.BAR = "BAZ" }; foo; $env.BAR
BAZ
```

cd affects the environment, so '--env' is required to change directory from within a command
```nu
> def --env gohome [] { cd ~ }; gohome; $env.PWD == ('~' | path expand)
true
```

Define a custom wrapper for an external command
```nu
> def --wrapped my-echo [...rest] { ^echo ...$rest }; my-echo -e 'spam\tspam'
spamspam
```

Define a custom command with a type signature. Passing a non-int value will result in an error
```nu
> def only_int []: int -> int { $in }; 42 | only_int
42
```

## Notes
This command is a parser keyword. For details, check:
  https://www.nushell.sh/book/thinking_in_nu.html

Chunks
44a824c3 (1st chunk of `commands/docs/def.md`)
Title: def: Define a Custom Command in Nushell
Summary
The `def` command in Nushell is used to define custom commands. It takes a command name, parameters, and a block of code as input. It supports flags like `--env` to persist environment changes and `--wrapped` to handle unknown flags. Examples demonstrate defining simple commands, commands with parameters, commands that modify the environment, custom wrappers for external commands, and commands with type signatures.