Post

Bash format date append static time

Bash format date append static time

I use this pattern in deployment scripts where I need to generate filenames or log markers with a consistent timestamp. The date command is flexible but the syntax for custom formats isn’t obvious. Saving the result to a variable with the $() syntax is the clean approach — avoids subshell issues.

Linux command to print current date and time

1
$ date

Parameters to get output from date in a custom format

1
$ date +'%m/%d/%Y'

To

  • append static time to the date
  • save it to a variable
  • echo the date time
1
2
3
$ NOW="$(date +'%m/%d/%Y') 20:00:00"

$ echo $NOW

For more examples visit stackoverflow concatenate string variables in Bash

This post is licensed under CC BY 4.0 by the author.