Need some help in fixing simple bash script below. What it does is to compare dates in epoch format using if else. The script does not work as I intended because it always goes to the first condition DEPLOY all the time.

Even if I set the deploymentDate variable to be greater than currentDate it still goes to the first condition.

Can anyone suggest on how to fix it?

currentDate=$(date +s% ) 

if [ "$currentDate" > "$deploymentDate" ] 
echo $XSL 



wrong date usage.

  • date +%s with GNU date or compatible, or
  • awk 'BEGIN{srand(); printf "%lu\n", srand()}' with most awk implementations (will be guaranteed by the next version of the POSIX specification)

wrong statement usage:

  • if ((currentDate > deploymentDate)) (ksh/zsh/bash)
  • if [ "$currentDate" -gt "$deploymentDate" ] (POSIX)

Alternatively, you could use UTC dates in the YYYY-mm-dd HH:MM:SS format and use lexical comparison:

currentDate=$(date -u +'%Y-%m-%d %T') 
deploymentDate='2017-12-17 13:10:01' # UTC
if [[ "$currentDate" > "$deploymentDate" ]] # ksh/bash/zsh

if expr "$currentDate" '>' "$deploymentDate" > /dev/null # POSIX

Or numerical comparison with YYYYmmddHHMMSS date formats, but you could run into problems on systems or shells (like mksh) using 32bit integers for arithmetics.

  • thanks for the answer. I tried using date +%s but it says line 2: +s%: command not found. See my revision – dimas Dec 18 '17 at 4:44
  • 1
    @dimas %s not s% also within command substitution $(date +%s) – αғsнιη Dec 18 '17 at 4:50
  • ahhh... thanks guys it's now working. What is the difference with single bracket and double bracket? I saw in some tutorial they are using single bracket for if else? – dimas Dec 18 '17 at 4:57
  • [[ x > y ]] (a ksh/zsh/bash operator) performs a lexical comparison (though typically, you'd have a problem if the deployment date was before September 2001), for a numerical comparison, use [ "$currentDate" -gt "$deploymentDate" ] (POSIX) or ((currentDate > deploymentDate)) (ksh, zsh, bash) – Stéphane Chazelas Dec 18 '17 at 12:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.