LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 11-01-2005, 01:38 PM   #1
essdeeay
Member
 
Registered: Aug 2004
Location: United Kingdom
Distribution: Debian
Posts: 88

Rep: Reputation: 15
Question How do I prevent echo eating spaces?


Hello

I'm writing a script in which I need to insert date/time stamps in exactly the same format as syslog (ie. %b %e %T). However, echo is eating the blank padded space for (%e).

I will insert the date/time stamp like this:
Code:
echo $DATE This is a date time stamp
and so have defined the DATE variable like this:
Code:
DATE=`date '+%b %e %T'`
This is where echo eats up the blank padded space.

If I were to insert the date/time stamp using "$DATE" instead of just $DATE, echo does not eat the spaces, but I don't want to do that and can't figure out how to include this in the actual DATE variable.

If I use sed (it's getting dirty and cumbersome now), I can do this (using echo -E $DATE):
Code:
DATE=`date '+%b %e %T' | sed 's/ /*/g'`
Nov**1*19:21:27
Which works as expected, but when I try and use an escaped space instead of an asterisk in the replacement text, it just won't work. Below, what I think should be being fed back to the echo command is:
Code:
Nov\ \ 1\ 19:21:27
but this is what I get instead:
Code:
DATE=`date '+%b %e %T' | sed 's/ /\\ /g'`
Nov 1 19:21:27
Any help would be appreciated.

Many thanks,
Steve
 
Old 11-01-2005, 02:07 PM   #2
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Re: How do I prevent echo eating spaces?

Quote:
Originally posted by essdeeay

If I were to insert the date/time stamp using "$DATE" instead of just $DATE, echo does not eat the spaces, but I don't want to do that and can't figure out how to include this in the actual DATE variable.
So echo $DATE doesn't work, but echo "$DATE" does... stupid question then, but why would you rather not do it that way? From all I can see, it's the only way you're going to be able to preserve the variable.
 
Old 11-01-2005, 02:52 PM   #3
essdeeay
Member
 
Registered: Aug 2004
Location: United Kingdom
Distribution: Debian
Posts: 88

Original Poster
Rep: Reputation: 15
Thanks for your reply rose_bud,

I was hoping to not have to use the " " to preserve the variable, because AFAIK you normally don't have to. However, it seems " " is the answer so that's the way I shall go.

Again, many thanks!
Steve
 
Old 11-01-2005, 03:11 PM   #4
rose_bud4201
Member
 
Registered: Aug 2002
Location: St Louis, MO
Distribution: Xubuntu, RHEL, Solaris 10
Posts: 929

Rep: Reputation: 30
Yeah, I understand why it's irritating, but I think this may not necessarily be a limitation of echo, but one of just bash in general (i.e. you have to surround function parameters with quotes if there's the slightest chance that the parameter could have a space in it, etc...). Bash tends to have some oversights at times =/
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Prevent Terminal Echo Using Python (for Passwords) taylor_venable Programming 4 07-05-2005 11:52 PM
Something is eating my memory benne Linux - Software 15 11-25-2004 06:02 AM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM
not eating watashiwaotaku7 General 12 09-29-2003 08:41 AM
Echo /devPrinting doesn't work, echo /usb/lp0 works, Testpage works, Printing doesn't Hegemon Linux - General 3 08-15-2002 01:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration