0

How do I specify the command date as a variable so that it is executed upon calling said variable?

DATE=`date +%H:%M:%S`
echo "$DATE"
sleep 5
echo "$DATE" # Date displays the original time, not the current one

--

EDIT 1: According to anubhava's suggestion:

freshdate[0]=`date +%H:%M:%S`

However, this does not result in:

echo "${freshdate[0]}"  # 19:25:50
sleep 5
echo "${freshdate[0]}"  # 19:25:50
Michael Gruenstaeudl
  • 1,609
  • 1
  • 17
  • 31

2 Answers2

2

Easiest is to wrap this command in a shell function like this:

dt() { date '+%H:%M:%S'; }

Then use it anywhere you want to access it as in:

echo "Time now is: $(dt)"
Time now is: 13:34:30
anubhava
  • 761,203
  • 64
  • 569
  • 643
-1

You can do

DATE=(date +%H:%M:%S)
${DATE[@]}
sleep 5
${DATE[@]}

and will show different times.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Yes, that prints the new time, but I would like to `echo` the new time (i.e., integrate it into a longer string). Something along the line of `echo "Foo bar baz $DATE[@]"`. How do I do that? – Michael Gruenstaeudl May 18 '17 at 17:27