> **Can't find what you're looking for?** Use `search_docs` on the docs MCP server at `https://www.getfoundry.sh/api/mcp` to find what you need.
>
> **Have feedback?** Use `submit_feedback` on the same MCP server.

## `envString`

### Signature

```solidity
function envString(string calldata key) external returns (string memory value);
function envString(string calldata key, string calldata delimiter) external returns (string[] memory values);
```

### Description

Reads an environment variable as `string` or `string[]`.

If the environment variable is not defined, Forge will fail with the following error message:

> \[FAIL. Reason: Failed to get environment variable `FOO` as type `string`: environment variable not found]

### Parameters

| Parameter   | Type     | Description                               |
|-------------|----------|-------------------------------------------|
| `key`       | `string` | The environment variable name             |
| `delimiter` | `string` | The delimiter used to separate array values (optional) |

### Returns

| Type       | Description                                  |
|------------|----------------------------------------------|
| `string`   | The environment variable value as a string   |
| `string[]` | The environment variable value as a string array |

### Examples

#### Single Value

With environment variable `STRING_VALUE=hello, world!`:

```solidity [test/EnvString.t.sol]
string memory key = "STRING_VALUE";
string memory expected = "hello, world!";
string memory output = vm.envString(key);
assertEq(output, expected);
```

#### Array

With environment variable `STRING_VALUES=hello, world!|0x7109709ECfa91a80626fF3989D68f67F5b1DD12D`:

```solidity [test/EnvString.t.sol]
string memory key = "STRING_VALUES";
string memory delimiter = "|";
string[2] memory expected = [
    "hello, world!",
    "0x7109709ECfa91a80626fF3989D68f67F5b1DD12D"
];
string[] memory output = vm.envString(key, delimiter);
for (uint i = 0; i < expected.length; ++i) {
    assert(keccak256(abi.encodePacked((output[i]))) == keccak256(abi.encodePacked((expected[i]))));
}
```

### Gotchas

:::tip[Delimiter Selection]
Choose a delimiter that doesn't appear in the string values, so that they can be correctly separated.
:::

:::note
You can put your environment variables in a `.env` file. During normal `forge`, `cast`, and `anvil` startup — not just `forge test` — Foundry reads the `.env` file in the project root discovered from your current working directory and, if the directories differ, the one in your current working directory. The `--root` option does not redirect this loading.

For ordinary text environment variables, values already set in your shell take precedence, followed by the project root's `.env`; the working directory's `.env` only supplies missing values. An existing empty value also takes precedence.

Quote values containing spaces, for example `GREETING="hello world"`. If a line cannot be parsed, variables loaded before that line remain set, but the remaining variables in that file are not loaded. Foundry continues startup and normally prints a warning; `--quiet` suppresses it.
:::

### Related Cheatcodes

* [`envOr`](/reference/cheatcodes/env-or) - Read environment variables with default values
* [`setEnv`](/reference/cheatcodes/set-env) - Set environment variables
* [`envBool`](/reference/cheatcodes/env-bool) - Read environment variables as bool
* [`envUint`](/reference/cheatcodes/env-uint) - Read environment variables as uint
