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 and shell scripting languages 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 08:44 AM
variables in shell viko Shell Programming and Scripting 2 03-03-2008 11:09 PM
Bash scripting question re: newlines retrovertigo Shell Programming and Scripting 4 07-06-2007 01:44 PM
Using shell variables In awk nortypig Shell Programming and Scripting 11 08-24-2006 01:48 AM
Setting up shell variables rachael UNIX for Dummies Questions & Answers 1 10-22-2001 12:16 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-15-2006
narcvs narcvs is offline
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
  #2 (permalink)  
Old 03-15-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2005
Posts: 683
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)

  #3 (permalink)  
Old 03-15-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
  
 

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


Code:
echo "$list" # now see what happens...

  #4 (permalink)  
Old 03-15-2006
narcvs narcvs is offline
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!
  #5 (permalink)  
Old 03-15-2006
narcvs narcvs is offline
Registered User
  
 

Join Date: Mar 2006
Posts: 3
Ah!

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

echo "$list"

works just fine.

Marcus
  #6 (permalink)  
Old 03-26-2009
pavanlimo pavanlimo is offline
Registered User
  
 

Join Date: Mar 2009
Posts: 10
This thread saved my life

You guys rock!

--
Pavan.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:52 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0