Search code examples
bashdatewhile-looptimestampunix-timestamp

Problem generating timestamps in a loop in Bash


When I run the command:

timestamp=$(date+"%s") ; echo "$timestamp"

there is no error.

However when I try to run it in a loop like:

while [  $COUNTER -lt 10 ]; do  ts=$(date+"%s") ; echo "$ts" ; let COUNTER=COUNTER+1 ;  done  

"-bash: date+%s: command not found" error is thrown. Why?


Solution

  • while [  $COUNTER -lt 10 ]; do  ts=$(date +"%s") ; echo "$ts" ; let COUNTER=COUNTER+1 ;  done
    

    you need to write it with a space $(date +"%s")