Date
以特定格式列印日期
我們想用以下日期格式列印 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