is-redirected
For platform.
Command Type
built-in
Signature
> is-redirected {flags}
Input/output types:
| input | output |
|---|---|
| nothing | bool |
Examples
Inside a custom command, report if the call’s return value is redirected (works in if).
> def pipetest [] { if (is-redirected) { "piped" } else { "display" } }; pipetest
Return true when the custom command is piped to another command.
> def pipetest [] { is-redirected }; pipetest | $in
true
Return true when the custom command result is collected into a variable.
> def pipetest [] { is-redirected }; let x = (pipetest); $x
true
Notes
This is a Nushell pipeline-destination check, not an OS TTY check.
Inside a custom command, is-redirected reports if that command’s return
value will be piped to another command, collected into a value (let,
subexpression), written to a file, or discarded — as opposed to being printed
via the normal display path.
Unlike looking at process stdout, this is stable for the entire command body, so
it works inside if (...) and let x = (...).
To test if process stdout is a terminal (scripts: ./script | cat), use
is-terminal --stdout instead.
Typical pattern for pretty-vs-data custom commands:
def mycmd [] {
if (is-terminal --stdout) and not (is-redirected) {
# human-friendly formatting
} else {
# structured data for pipelines
}
}
Ported from nushell/nushell.github.io. Copyright (c) 2021 Nushell Project. MIT License.