0

I'm writing a bash script in FreeBSD that appends commands to a log file. Before I execute the commands that will append the log data, I want to print a line in the file which shows the current date above the data, like this:

---Tue Aug 20 17:26:37 EDT 2019---

I know that I can use the date command to output the timestamp, but I'm not sure how to include the "---" before and after the timestamp to add to the file. What's the simplest way to do this?

4
  • So many choices. printf '-- %s --' "$(date)" or printf '-- '; date | tr -d \\n; printf ' --' Commented Aug 20, 2019 at 21:43
  • date | sed -e 's/^/-- /' -e 's/$/ --/' Commented Aug 20, 2019 at 21:44
  • 1
    If your implementation of date supports it: date +'-- %a %b %d %T %Z --' Commented Aug 20, 2019 at 21:48
  • Why the pain? %FT%T. Commented Aug 21, 2019 at 9:37

1 Answer 1

2

You can pass a format string to date:

date '+---%a %b %e %H:%M:%S %Z %Y---'
Sign up to request clarification or add additional context in comments.

1 Comment

Thats perfect! Thank you all for the feedback :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.