The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Accessing Shell Variables in awk or sed nasersh Shell Programming and Scripting 3 05-05-2008 04:44 AM
variables in shell viko Shell Programming and Scripting 2 03-03-2008 07:09 PM
Bash scripting question re: newlines retrovertigo Shell Programming and Scripting 4 07-06-2007 09:44 AM
Using shell variables In awk nortypig Shell Programming and Scripting 11 08-23-2006 09:48 PM
Setting up shell variables rachael UNIX for Dummies Questions & Answers 1 10-21-2001 08:16 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-15-2006
Registered User
 

Join Date: Mar 2006
Posts: 3
Newlines in shell variables

Hello,

I'm trying to create a shell variable with newlines inside it, so that when I echo the variable and pipe it to, say, awk, it output with the newlines. Why is this so problematic? I frankly don't know, but BASH seems to be stripping my variable of newlines. Here's an example

$ cat script.sh
#!/bin/bash

list="hello " # first hello
list=$list`echo ""` # should then put a newline after hello
list=$list'

' # This should too
list=$list"world" # and world on the next row

echo $list # now see what happens...
$ ./script.sh
hello world


WHY DOES IT TAKE AWAY MY NEWLINES? I solved it by creating a temporary file, output everything there, and then read from the file, but I don't want to do that!

Does anyone know how to store newlines in a shell varabile? Thanks a bunch,

Marcus
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-15-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
bash gives alot of help here. From the bash man page:
Code:
     echo [-neE] [arg ...]
          Output the args, separated by  spaces,  followed  by  a
          newline.   The  return  status  is  always 0.  If -n is
          specified, the trailing newline is suppressed.  If  the
          -e  option  is  given,  interpretation of the following
          backslash-escaped characters is enabled.  The -E option
          disables the interpretation of these escape characters,
          even on systems where they are interpreted by  default.
          echo  does not interpret -- to mean the end of options.
          echo interprets the following escape sequences:
          \a   alert (bell)
          \b   backspace
          \c   suppress trailing newline
          \e   an escape character
          \f   form feed
          \n   new line
          \r   carriage return
          \t   horizontal tab
          \v   vertical tab
          \\   backslash
          \nnn the character whose ASCII code is the octal  value
               nnn (one to three digits)
          \xnnn
               the character whose ASCII code is the  hexadecimal
               value nnn (one to three digits)
Reply With Quote
  #3 (permalink)  
Old 03-15-2006
Registered User
 

Join Date: Jan 2005
Posts: 682
By the way, your specific issue is that you do not have quotes around your variable:

Code:
echo "$list" # now see what happens...
Reply With Quote
  #4 (permalink)  
Old 03-15-2006
Registered User
 

Join Date: Mar 2006
Posts: 3
Thanks tmarikle, but that unfortunatly doesn't do it.

$ cat script.sh
#!/bin/bash

list="hello " # first hello
list=$list`echo -e "\ntest\n "` # As per advice from tmarikle
list=$list"world" # and world on the next row

echo $list # now see what happens...
$ ./script.sh
hello test world
$

I think it has something to do with bash variables, being stripped of newlines. It is SO frustrating!
Reply With Quote
  #5 (permalink)  
Old 03-15-2006
Registered User
 

Join Date: Mar 2006
Posts: 3
Ah!

Hah, silly me. Yes, of course. Thanks a bunch,

echo "$list"

works just fine.

Marcus
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 02:55 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0