Home Explore Blog CI



nushell

commands/docs/if.md
345830d917097c53fd902fe1f25426649b1435c194524603000000030000049a
---
title: if
categories: |
  core
version: 0.104.0
core: |
  Conditionally run a block.
usage: |
  Conditionally run a block.
editLink: false
contributors: false
---
<!-- This file is automatically generated. Please edit the command in https://github.com/nushell/nushell instead. -->

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

<div class='command-title'>Conditionally run a block.</div>

## Signature

```> if {flags} (cond) (then_block) (else_expression)```

## Parameters

 -  `cond`: Condition to check.
 -  `then_block`: Block to run if check succeeds.
 -  `else_expression`: Expression or block to run when the condition is false.


## Input/output types:

| input | output |
| ----- | ------ |
| any   | any    |
## Examples

Output a value if a condition matches, otherwise return nothing
```nu
> if 2 < 3 { 'yes!' }
yes!
```

Output a value if a condition matches, else return another value
```nu
> if 5 < 3 { 'yes!' } else { 'no!' }
no!
```

Chain multiple if's together
```nu
> if 5 < 3 { 'yes!' } else if 4 < 5 { 'no!' } else { 'okay!' }
no!
```

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

Chunks
16c1d33a (1st chunk of `commands/docs/if.md`)
Title: if Command in Nushell
Summary
The `if` command in Nushell allows conditional execution of code blocks based on a given condition. It takes a condition, a 'then' block to execute if the condition is true, and an optional 'else' expression or block to execute if the condition is false. Multiple `if` statements can be chained together for more complex logic.