views:

140

answers:

5

Cygwin user here (though if there's a suitable solution I will carry it over to K/Ubuntu, which I also use).

I have a Welcome message in my .bashrc that looks like the following:

SAD=(`date +%A-%B-%d-%Y`)
DUB=(`date -u +%k:%M`)
printf "Today's Date is: ${SAD}.\n"
printf "Dublin time is now ${DUB}. (24-Hour Clock)\n"

After numerous attempts to use whitespaces in the variable SAD above, I gave in and used hyphens. But I am, understandably, not satisfied with this band-aid solution. The problem, as I recall, was that every time I tried using quoted space, \s or some similar escape tag, along with the variables listed in the appropriate section of the GNU manpage for date, the variable for Year was either ignored or returned an error. What I do'nt want to have to do is resort to the full string as returned by date, but rather to keep the order in which I have things in the code above.

As I write this, it occurs to me that setting the IFS around this code for the Welcome message may work, provided I return it to defaults afterwards (the above appears at lines 13-17 of a 68-line .bashrc). However, I can't recall how to do that, nor am I sure that it would work.

Generally-speaking, .bashrc files are in valid BASH syntax, aren't they? Mine certainly resemble the scripts I've either written myself or tested from other sources. All I'm missing, I suppose, is the code for setting and unsetting the field separators around this message block.

No doubt anyone who comes up with a solution will be doing a favor not only to me, but to any other relative newbies on the Net taking their first (or thirteenth through seventeenth) steps in the world of shell scripting.

BZT

+1  A: 

the problem is your array declaration.

SAD=(date +%A-%B-%d-%Y) just means you are putting the string "date" as element 0, and "+%A-%B-%d-%Y" as element 1. see for yourself

$ SAD=(date +%A-%B-%d-%Y)  #<-- this is an array declaration
$ echo ${SAD[0]}
date
$ echo ${SAD[1]}
+%A-%B-%d-%Y

if you want the value of "date" command to be in a variable, use $(..), eg

$ SAD=$(date +%A-%B-%d-%Y)
$ echo ${SAD}
Thursday-February-18-2010
ghostdog74
A: 

To get spaces, you need to quote the argument to date so that it's a single string. You're also erroneously declaring SAD and DUB as arrays, when what you really meant to do was evaluate them. Try this:

[/tmp]> $(date "+%A %B %d, %Y")
Thursday February 18, 2010
John Feminella
+1  A: 

Putting

SAD=$(date "+%A %B %d %Y")
DUB=$(date -u +%k:%M)
echo "Today's Date is: ${SAD}."
echo "Dublin time is now ${DUB}. (24-Hour Clock)"

in my .bash_profile prints

Today's Date is: Thursday February 18 2010.
Dublin time is now 12:55. (24-Hour Clock)

I think that's what you want.

Christian
The quickest responses to any question I've posted anywhere on the Net. But for the lag of StackOverflow's ad server, I might have seen them as they were stacking (npi) up. My thanks to all the respondents besides myself. BZT
silversleevesx
A: 

date +%A\ %B\ %d\ %Y

bhups
you can use quotes
ghostdog74
A: 

I found the combination that works:
SAD=$(date "+%A %B %d %Y")
echo $SAD
Thursday February 18 2010

Yet another instance when:

  1. It pays to ask a question
  2. It helps to know where to put your double quotes.
    date obviously does not know from quoted space, but Bash does, so
    it's a matter of "whispering in the right ear."

Thank you ghostdog74.

BZT

silversleevesx