---
description: Reads an environment variable with a default fallback value
---

## `envOr`

### Signature

```solidity
function envOr(string calldata key, bool defaultValue) external returns (bool value);
function envOr(string calldata key, uint256 defaultValue) external returns (uint256 value);
function envOr(string calldata key, int256 defaultValue) external returns (int256 value);
function envOr(string calldata key, address defaultValue) external returns (address value);
function envOr(string calldata key, bytes32 defaultValue) external returns (bytes32 value);
function envOr(string calldata key, string calldata defaultValue) external returns (string memory value);
function envOr(string calldata key, bytes calldata defaultValue) external returns (bytes memory value);
function envOr(string calldata key, string calldata delimiter, bool[] calldata defaultValue) external returns (bool[] memory value);
function envOr(string calldata key, string calldata delimiter, uint256[] calldata defaultValue) external returns (uint256[] memory value);
function envOr(string calldata key, string calldata delimiter, int256[] calldata defaultValue) external returns (int256[] memory value);
function envOr(string calldata key, string calldata delimiter, address[] calldata defaultValue) external returns (address[] memory value);
function envOr(string calldata key, string calldata delimiter, bytes32[] calldata defaultValue) external returns (bytes32[] memory value);
function envOr(string calldata key, string calldata delimiter, string[] calldata defaultValue) external returns (string[] memory value);
function envOr(string calldata key, string calldata delimiter, bytes[] calldata defaultValue) external returns (bytes[] memory value);
```

### Description

A non-failing way to read an environment variable of any type: if the requested environment key does not exist, `envOr()` will return a default value instead of reverting (works with arrays too).

The returned type is determined by the type of `defaultValue` parameter passed.

### Parameters

| Parameter      | Type     | Description                                   |
|----------------|----------|-----------------------------------------------|
| `key`          | `string` | The environment variable name                 |
| `defaultValue` | varies   | The default value to return if the key is not set |
| `delimiter`    | `string` | The delimiter used to separate array values (for array variants) |

### Returns

The return type matches the type of `defaultValue`.

### Examples

#### Single Value

If the environment variable `FORK` is not set, you can specify it to be `false` by default:

```solidity [test/EnvOr.t.sol]
bool fork = vm.envOr("FORK", false);
```

Or set a default owner address:

```solidity [test/EnvOr.t.sol]
address owner;

function setUp() {
    owner = vm.envOr("OWNER", address(this));
}
```

#### Array

If the environment variable `BAD_TOKENS` is not set, you can specify the default to be an empty array:

```solidity [test/EnvOr.t.sol]
address[] badTokens;

function envBadTokens() public {
    badTokens = vm.envOr("BAD_TOKENS", ",", badTokens);
}
```

Or using a local variable:

```solidity [test/EnvOr.t.sol]
function envBadTokens() public {
    address[] memory defaultBadTokens = new address[](0);
    address[] memory badTokens = vm.envOr("BAD_TOKENS", ",", defaultBadTokens);
}
```

### Gotchas

:::note
The parsing of the environment variable will be done according to the type of `defaultValue` (e.g. if the default value type is `uint` - the environment variable will be also parsed as `uint`).
:::

:::tip[Type Casting]
Use explicit casting for literals to specify the type of the default variable:
- `uint(69)` will return a `uint`
- `int(69)` will return an `int`
- `string("")` returns a `string`
- `bytes("")` returns `bytes`
:::

:::warning[Dynamic Arrays Only]
Use dynamic arrays (`bool[]`) instead of fixed-size arrays (`bool[4]`) when providing default values. Only dynamic arrays are supported.
:::

### Related Cheatcodes

- [`envBool`](/reference/cheatcodes/env-bool) - Read environment variables as bool (reverts if not set)
- [`envUint`](/reference/cheatcodes/env-uint) - Read environment variables as uint (reverts if not set)
- [`envString`](/reference/cheatcodes/env-string) - Read environment variables as string (reverts if not set)
- [`setEnv`](/reference/cheatcodes/set-env) - Set environment variables
