0

Why this difference between echo $Date and echo "$(date +%s_%kh%M_le_%a_%d_%b_%Y)"?

beginning=$(date +%s)
diff="0"
rest="$1"
tmp="0"
now="0"
Date=`date +%s_%kh%M_le_%a_%d_%b_%Y`

echo -n "$1"
while [ "$diff" -lt "$1" ]
do
    let "now=$(date +%s)"
    let "diff=now-beginning-1"
    let "tmp=$1-diff"
    if [ "$tmp" -lt "$rest" ]
    then
        echo
        echo $Date
        echo "$(date +%s_%kh%M_le_%a_%d_%b_%Y)"
        let "rest=$1-diff"
        echo -n ", $rest"
    fi
done

echo ""

in output:

concatenatorus@gwenn-ha-du ~ $ countdown.sh 5
5
1425230747_18h25_le_dim._01_mars_2015
1425230749_18h25_le_dim._01_mars_2015
, 4
1425230747_18h25_le_dim._01_mars_2015
1425230750_18h25_le_dim._01_mars_2015
, 3
1425230747_18h25_le_dim._01_mars_2015
1425230751_18h25_le_dim._01_mars_2015
, 2
1425230747_18h25_le_dim._01_mars_2015
1425230752_18h25_le_dim._01_mars_2015
, 1
1425230747_18h25_le_dim._01_mars_2015
1425230753_18h25_le_dim._01_mars_2015
, 0

I use :

echo $BASH
/bin/bash
3
  • 2
    What is the problem? There are 8 commands between Date=... and echo "$(date... which operate more than 2 sec. What are you asking for?
    – Costas
    Mar 1, 2015 at 18:38
  • Start the script with sh -x countdown.sh 5.
    – ott--
    Mar 1, 2015 at 18:51
  • The question seems very clear to me and perfectly answered by Ariel.
    – asoundmove
    Mar 1, 2015 at 20:22

2 Answers 2

2

The difference is because you run

Date=`date +%s_%kh%M_le_%a_%d_%b_%Y`

at the very beginning of the script. That stores that date/time in a shell variable called Date... and then you execute

echo "$(date +%s_%kh%M_le_%a_%d_%b_%Y)"

in a loop... the last command queries and prints a new time value in every iteration, the echo $Date prints always the same value!

0

In your script you have used back quotes in Date=date +%s_%kh%M_le_%a_%d_%b_%Y

And in echo statement double quotes are used. In your script it yields the same result.

Back quotes are used for evaluating expressions only and double quotes are used for evaluating expressions as well as displaying messages.

In case of your script it really doesn't matter whether you use double quotes or back quotes.

Not the answer you're looking for? Browse other questions tagged .