Your understanding is incorrect. When you store the content `"hello\nworld"` into a variable, the `\n` is interpreted literally. Only if you invoke tools such as `printf` or `echo` with `-e` flag, they expand those backslash sequences when printing to console.

In your case, you want to pass the variable to the environment with newline character expanded, suggest using the [ANSI C style `$'...'` quoting operator][1] of ksh93, now supported by several other shells including bash and zsh for this:

```bash
env FOO=$'hello\nworld' ruby -e 'puts ENV["FOO"]'
```

Or if portability is an issue, a trivial solution is to put those newlines where you want them

```bash
f="hello
world"

env FOO="$f" ruby -e 'puts ENV["FOO"]'
```



 [1]: https://www.gnu.org/software/bash/manual/html_node/ANSI_002dC-Quoting.html