LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-09-2004, 11:30 PM   #1
tisleti
LQ Newbie
 
Registered: Sep 2004
Location: North Carolina
Distribution: Solaris 8, DSL (kernel 2.4.26)
Posts: 1

Rep: Reputation: 0
stupid bash question


Hi all-

I am pretty new to *nix and I have a possibly stupid question. I have a need to capture, as a variable, the month and date as it is displayed when you issue a date command.

e.g. :
bash prompt$ date
bash prompt$ Thu Sep 9 23:14:21 2004
(emphasis mine - since the date is < 10, there are 2 white spaces between Sep and 9)

When I issue a cut command after date (date | cut -c5-10), I get the exact 6-digit string (Sep 9) result that I am seeking. HOWEVER, when I try to capture that result into a variable, things do not come out the same. It seems that the process of storing the result of the date | cut command in a variable strips off one of the spaces.

e.g. :
bash prompt$ JJ=$(date | cut -c5-10)
OR
bash prompt$ JJ=`date | cut -c5-10`
bash prompt$ echo $JJ
bash prompt$ Sep 9
(There now only one white space between Sep and 9. Where did the other white space go?)

What is it that I am doing wrong? Your help would be appreciated.

Last edited by tisleti; 09-09-2004 at 11:34 PM.
 
Old 09-09-2004, 11:44 PM   #2
Demonbane
LQ Guru
 
Registered: Aug 2003
Location: Sydney, Australia
Distribution: Gentoo
Posts: 1,796

Rep: Reputation: 47
I think there's only one space even if the day is single digit
 
Old 09-10-2004, 02:54 AM   #3
mindspin
LQ Newbie
 
Registered: Oct 2003
Location: NL.
Distribution: Fedora Core 2
Posts: 29

Rep: Reputation: 15
Isnt it the same as with coding in HTML?
When you put more then one space after another, it is just ignored. You should try using the code for space like in HTML it is: &nbsp;

Hope this might help you out
 
Old 09-10-2004, 04:21 AM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
Two things will give you what you want.
1. Change the IFS variable so that a space character isn't a field seperator.
2. Place the variable in double quotes.
Code:
echo $JJ
Sep 9
echo "$JJ"
Sep  9
 
Old 09-10-2004, 06:34 AM   #5
Electro
LQ Guru
 
Registered: Jan 2002
Posts: 6,042

Rep: Reputation: Disabled
You do not have to go through all of that cut mess. You can do something as simple as:
JJ="`date +%b%e`"
or
JJ="`date "+%b %e"`"

The double quotes will preserve the spaces.

sheesh...RTFM
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Stupid, stupid question; I lost Klaptop. :( Surfrider Slackware 2 08-31-2005 10:12 PM
Stupid Dumb Stupid Question... drigz Linux - Software 3 09-23-2004 04:09 PM
Stupid bash problem ericcarlson Linux - Newbie 3 07-17-2004 03:19 AM
bash script - even more stupid question than last time. webamoeba Linux - Newbie 2 02-12-2004 04:03 AM
Very, Very Stupid Question Lampei Linux - Newbie 2 03-17-2003 09:33 AM


All times are GMT -5. The time now is 04:13 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration