-   Programming (
-   -   Shell Script - Date/Time (

runnerpaul 08-19-2008 09:27 AM

Shell Script - Date/Time

In shell scripting how would I add the date/time to a log file?


Agrouf 08-19-2008 09:31 AM

Do you mean 'add' as 'append the date at the end of a log file'?
If that is so, do that:

date >> file.log

colucix 08-19-2008 09:34 AM

If you want to put the date in the log filename, use command substitution. For example

command >> /path/to/$(date +%Y%m%d)_file.log
will append the output of the command to /path/to/20080819_file.log.

b0uncer 08-19-2008 09:39 AM

Also take a look at 'date' command's man page:

man date
(or see this page instead)

Notice the '+FORMAT' there? See the lower part of the man page for formatting strings and combine them into the command to be able to append the date/time in the format you want/need (the default format given by plain 'date' might not be what you want).

That method, redirecting the output of a command to a file using '>>', is fairly common and works in DOS too, exactly the same way (except that there might not be 'date' command available). Two greater than -chars ('>>') means 'append' while one ('>') alone clears the file contents before writing the new content, so be aware:

date >> file.log #existing content remains, date is added to the end
date > file.log #existing content vanishes, now file only contains date's output

For more information about what you can do with your shell ('tricks' if you like), see your shell's man page, for example

man bash
EDIT: of course that means the shell you use to run your script; you should define it in the first line of the script file, for example


matthewg42 08-19-2008 12:10 PM

I usually write a function for writing to log files, something like this:




logit () {
    echo "$(date +%Y%m%d-%T) $scriptname : $@" >> "$logfile"

logit "starting script"


logit "ending script"

The log file will then contain lines formatted like this:

20080819-17:09:49 myscript : starting script
20080819-17:09:50 myscript : ending script

runnerpaul 08-20-2008 04:20 AM

Hi I tried the following:


#!/sbin/sh -
log() {
    /usr/bin/echo "$(date +%Y%m%d-%T) $1" >> ${LOGFILE}

But in mylogs instead of printing the date/time I got the following:


$(date +%Y%m%d-%T)

matthewg42 08-20-2008 05:43 AM

I can think of two possible reasons:
  1. Although you have put double quotes in your post, maybe you used single quotes in your script, like this:

    /usr/bin/echo '$(date +%Y%m%d-%T) $1' >> ${LOGFILE}
    Single quotes won't work, since they mean that the contents of the quotes are a literal string, and no expansion of the $( ... ) will be done.
  2. You are using a strange shell which does not expand $( ... ). What is /sbin/sh? If you do not use bash, you should explicitly state what you are using in posts to LQ - most people here will assume you are using bash unless you tell them otherwise (me included).

colucix 08-20-2008 05:47 AM

From your sha-bang it seems you're using the Bourne shell, where the syntax $(command) for command substitution is not recognized, since it is specific to bash. Use back ticks instead:

/usr/bin/echo "`date +%Y%m%d-%T` $1" >> ${LOGFILE}
Edit: matthew, you were faster than me. You're right, it is a strange location for the shell executable, anyway if the shell does not complain about it, the sha-bang is correct.

matthewg42 08-20-2008 06:34 AM

For the record, both backtick execution

and $(command) execution

are different notations to achieve more or less the same thing in bash and many other borne shell derivatives (including zsh, ksh and dash). i.e. "execute a command, and put the output of the command (from standard output) here".

In all these shells you can also use the `backtick` notation. I prefer not to instruct people using backticks for two reasons:
  • They are extremely easy to confuse with single quotes, and often lead to a lot of back and forth because the font which is used to display the message either doesn't discriminate at all between the two characters, or the reader does not pay close enough attention.
  • Backtick execution is not nestable. Using $(...), you can have an "inner" execution, which is done first. This is not such a huge reason... it is not a commonly used feature, although I will say that I have used it in the past. Sure enough it's possible to do the same thing in two stages with backticks, but sometimes it's nice to be able to nest these things.

colucix 08-20-2008 08:45 AM

I would add that the syntax $(command) interprets the double backslash correctly, or at least in the way you expect it, as an escaped backslash.

All times are GMT -5. The time now is 05:19 PM.