Search code examples
datestring-formattingbashifs

BASH: How do you "split" the date command?


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


Solution

  • 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.