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

The Cygwin user is here (although if there is a suitable solution, I will port it to K / Ubuntu, which I also use).

I have a welcome message in my .bashrc that looks like this:

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

After numerous attempts to use spaces in the SAD variable above, I gave and used hyphens. But I, of course, am not satisfied with the decision of this group. The problem, as far as I remember, was that every time I tried to use the specified space, \ s or some similar escape tag, along with the variables listed in the corresponding section of the GNU man page for date , the variable for Year was either ignored or error returned. What I don't want to do is resort to the full line returned by date , and keep the order in which I have things in the code above.

As I write this, it occurs to me that setting IFS around this code for a greeting message may work if after that I return it to default (the above appears in lines 13-17 of the 68-line .bashrc), however I don’t I can remember how to do this, and I'm sure it will work.

Generally speaking, .bashrc files are in valid BASH syntax, aren't they? Mine, of course, is similar to the scripts that I either wrote or tested from other sources. All that I am missing, I suppose, is the code to set and remove the field separators around this message block.

Without a doubt, anyone who comes up with a solution will provide a service not only to me, but to any other newcomer relatives on the Web who take their first (or thirteenth to seventeenth) steps in the world of shell scripts.

BZT

+3
5

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

.bash_profile

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

, .

+4

- .

SAD=(date +%A-%B-%d-%Y) , "date" 0 "+% A-% B-% d-% Y" 1.

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

, "date" , $(..),

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

, date, . SAD DUB , , , . :

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

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

0

, :
SAD=$(date "+%A %B %d %Y")
echo $SAD
Thursday February 18 2010

, :

  • This helps to know where to put double quotes.
    the date clearly does not know from the quoted space, but Bash does, so this is the question "whispers in the right ear."

Thanks ghostdog74.

BZT

0
source

Source: https://habr.com/ru/post/1733343/


All Articles