Date

以特定格式列印日期

  • March 22, 2018

我們想用以下日期格式列印 bash 腳本中的日誌

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

echo在這種情況下您不需要:

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

如果$task$1包含%,則必須將它們替換為%%first ,以免它們被解釋為格式序列date。這可以bash

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

來自man date

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

引用自:https://unix.stackexchange.com/questions/432782