def
for core
Define a custom command.
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 nameparams
: parametersblock
: body of the definition
Input/output types:
input | output |
---|---|
nothing | nothing |
Examples
Define a command and run it
> def say-hi [] { echo 'hi' }; say-hi
hi
Define a command and run it with parameter(s)
> def say-sth [sth: string] { echo $sth }; say-sth hi
hi
Set environment variable by call a custom command
> def --env foo [] { $env.BAR = "BAZ" }; foo; $env.BAR
BAZ
Define a custom wrapper for an external command
> def --wrapped my-echo [...rest] { echo $rest }; my-echo spam
╭───┬──────╮
│ 0 │ spam │
╰───┴──────╯
Notes
This command is a parser keyword. For details, check: https://irsh.vercel.app/book/thinking_in_rsh.html