Skip to main content
added 87 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

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 inof ksh93, now supported by several other shells including bash and zsh 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"]' 

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"]' 

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 of ksh93, now supported by several other shells including bash and zsh 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"]' 
added 176 characters in body
Source Link
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"]' 

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"]' 

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"]' 
Source Link
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"]'