I am writing a bash script for backup with log storage.
I use my defined function as follows:
logit () { echo " $1 $2 " >> /path/to/log }
For
logit 'Starting Backup' $(date +'%D %T')
I get this output:Starting Backup 01/11/22
so the time is missing, apparently the
stdout
function has shortened it.With
echo $(date +'%D %T')
I also get the time in thestdout
.I would also like to use my function for logs, e.g.
logit 'DB-LOGS' $(cat /path/to/sql)
results in
DB-LOG mysqldump:
Again, some stdout is missing here.
What should I change or add to the function to get complete output?
printf '%s %(%D %T)T\n' 'Starting backup' -1
. Inzsh
, you can do time stamping with prompt expansion or thestrftime
builtin. – Stéphane Chazelas Jan 11 '22 at 12:42