6

I have a env.sh which contains:

export MULTI_LINES="line1\nline2\nline3"

Then I use source to run it

source env.sh

When I check the environment variable:

printenv | grep MULTI_LINES

it shows

MULTI_LINES=line1\nline2\nline3

How to escape "\n" into line break?

Tiancheng Liu
  • 782
  • 2
  • 9
  • 22

1 Answers1

8

If using bash, use this form to make \n interpreted as newline:

export MULTI_LINES=$'line1\nline2\nline3'

As per man bash

Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

Or you can just place value in multiline:

export MULTI_LINES="line1
line2
line3"
anubhava
  • 761,203
  • 64
  • 569
  • 643