We want to print the log in bash script with the following date format

 2018-03-22 09:54:05,$task   ( $task is a counter of command )

 echo " `date +% .... ,$task` INFO $1"   ( $1 is the $1 argument that we want to print )


 2018-03-22 09:54:05,001 INFO script begin to start server
 2018-03-22 09:54:05,001 INFO script begin to start agent
  • 1
    What have you tried? The date manpage lists the various formatting directives it supports. – Stephen Kitt Mar 22 '18 at 10:03
  • echo " date +%G"-"%d"-"%H " – yael Mar 22 '18 at 10:09

You don't need echo in this instance:

date +"%F %T,$task INFO $1"

If $task or $1 contains %, then these has to be replaced with %% first so that they are not interpreted as format sequences by date. This could be done in bash with

date +"%F %T,${task//%/%%} INFO ${1//%/%%}"

From man date:

%F     full date; same as %Y-%m-%d
%T     time; same as %H:%M:%S

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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