Skip to main content
2 of 3
added 176 characters in body
Inian
  • 13.1k
  • 2
  • 42
  • 56

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 ANSI C style quoting in bash for this

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

f="hello world" env FOO="$f" ruby -e 'puts ENV["FOO"]' 
Inian
  • 13.1k
  • 2
  • 42
  • 56