Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, How can I include a new line character in a variable in a bash shell script? test.sh ============= myvar="line1\nline2" echo $myvar # gives line1\nline2 Doesn't work...and I have already ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jun 2006
    Posts
    4

    Question [SOLVED] [Bash] New line character in variable


    Hi,

    How can I include a new line character in a variable in a bash shell script?

    test.sh
    =============
    myvar="line1\nline2"
    echo $myvar # gives line1\nline2


    Doesn't work...and I have already tried many different combinations (\r\n, etc etc).

    Thanks in advance.

    Michael

  2. #2
    Just Joined!
    Join Date
    May 2006
    Posts
    19
    $xxx = "abcd\
    efgh"

    don't sure it works , but try (=

  3. #3
    Just Joined!
    Join Date
    Jun 2006
    Posts
    4
    Quote Originally Posted by SergeS
    $xxx = "abcd\
    efgh"

    don't sure it works , but try (=
    Thanks for your suggestion but it doesn't work.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Dec 2006
    Posts
    1

    Echo a newline character

    Since newline, tab and etc are escape characters.

    try this:

    echo -e "line1\nline2"

    -e to enable output escape characters

    This should works.

  6. #5
    Just Joined!
    Join Date
    Jul 2008
    Posts
    1

    Thanks

    echo -e works.

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •