<---- template headericclude ----->
Adding \n newline control character onto a bash string
FedoraForum.org - Fedora Support Forums and Community
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2006
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding \n newline control character onto a bash string

    I'm trying to build a small text file on-the-fly with bash. It has several lines, each one ending with a newline character.

    So far I have:

    Code:
    [root@F10]# LINE1="This is line 1"
    [root@F10]# LINE2="This is line 2"
    [root@F10]# LINE3="This is line 3"
    [root@F10]#
    [root@F10]# FILE="${LINE1}${LINE2}${LINE3}"
    [root@F10]# echo $FILE
    This is line 1This is line 2This is line 3
    [root@F10]#
    [root@F10]# FILE="${LINE1}"\n"${LINE2}"\n"${LINE3}"
    [root@F10]# echo $FILE
    This is line 1nThis is line 2nThis is line 3
    I can't get the \n newline characters onto the end of the lines. Am I missing something?

    I want the output to look like:

    Code:
    This is line 1
    This is line 2
    This is line 3

  2. #2
    Join Date
    Mar 2009
    Location
    Wonder Land
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CD-RW
    I'm trying to build a small text file on-the-fly with bash. It has several lines, each one ending with a newline character.

    So far I have:

    Code:
    [root@F10]# LINE1="This is line 1"
    [root@F10]# LINE2="This is line 2"
    [root@F10]# LINE3="This is line 3"
    [root@F10]#
    [root@F10]# FILE="${LINE1}${LINE2}${LINE3}"
    [root@F10]# echo $FILE
    This is line 1This is line 2This is line 3
    [root@F10]#
    [root@F10]# FILE="${LINE1}"\n"${LINE2}"\n"${LINE3}"
    [root@F10]# echo $FILE
    This is line 1nThis is line 2nThis is line 3
    I can't get the \n newline characters onto the end of the lines. Am I missing something?

    I want the output to look like:

    Code:
    This is line 1
    This is line 2
    This is line 3
    Hello CD-RW,

    Instead of invoking:
    Code:
    echo $FILE
    try using:
    Code:
    echo -e $FILE
    the -e option enables interpretation of backslash escapes....

    Code:
    If -e is in effect, the following sequences are recognized:
    
           \0NNN  the character whose ASCII code is NNN (octal)
    
           \\     backslash
    
           \a     alert (BEL)
    
           \b     backspace
    
           \c     suppress trailing newline
    
           \f     form feed
    
           \n     new line
    
           \r     carriage return
    
           \t     horizontal tab
    
           \v     vertical tab
    Also consult man page for 'echo'....
    Whatever may be the outcome.... please post your problem/soultion

    -fav0nius

  3. #3
    stevea Guest
    Yeah well - decent advice favOnious except "echo" is both a bash shell built-in AND a separate coreutils binary.
    The" man echo" page only refers to the coreutils version. See "man bash" for the normally used version.

  4. #4
    Join Date
    Mar 2009
    Location
    Wonder Land
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ooh... it just skipped from my mind ...
    Thanks for reminding

    -fav0nius
    Last edited by fav0nius; 2nd July 2009 at 09:43 AM.

  5. #5
    Join Date
    Nov 2006
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thanks for your replies and help fav0nius and stevea.

    Here is the solution to my problem

    Code:
    [root@F10]# LINE1="This is line 1"
    [root@F10]# LINE2="This is line 2"
    [root@F10]# LINE3="This is line 3"
    [root@F10]# FILE=${LINE1}'\n'${LINE2}'\n'${LINE3}
    [root@F10]# echo -e $FILE
    This is line 1
    This is line 2
    This is line 3
    [root@F10]# echo -e $FILE > newfile.txt
    [root@F10]# cat newfile.txt
    This is line 1
    This is line 2
    This is line 3
    So this will allow me to build a text file on-the-fly, directly from a bash script.

Similar Threads

  1. Newline character
    By joegumbo in forum Using Fedora
    Replies: 4
    Last Post: 30th May 2007, 02:42 AM
  2. Long commands in bash, but no newline
    By buskmann in forum Using Fedora
    Replies: 3
    Last Post: 10th January 2006, 03:28 AM
  3. Bash help: Find first n characters in a string?
    By leaded in forum Programming & Packaging
    Replies: 3
    Last Post: 29th December 2004, 06:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
[[template footer(Guest)]]