26

I am trying to display a date time in the form of

07/08/2013 16:52:13

by using command in bash script:

dt=`date '+%d/%m/%Y_%H:%M:%S'`

which variable is used to populate a CSV file.

The only char accepted is "_" (underscore) or "-" between date and time, output is

07/08/2013_16:52:13

How can I get a space in between date and time? I tried almost (I hope) everything.

closed as unclear what you're asking by Gilles, slm, jasonwryan, user26112, Anthon Aug 9 '13 at 5:28

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 5
    ” and “-” are the only characters accepted by _who? What stops you replacing the “_” with “ ” in date's format string? – manatwork Aug 8 '13 at 12:52
  • Nothing prevents the inclusion of the " " in the command, they just don't appear in the output. Try date +"%b %e" and you will get "Sep 4" (one space) for today, not the desired 2. I'm using it in a script and my solution is to do the operation in 3 steps (I actually combine them into one line of code but you get the idea). – Jim2B Sep 4 '15 at 16:18
  • With all due respect, how did this question get 20 votes?  And how did it get into the Reopen Queue? – G-Man Jan 19 at 2:20
43
#!/bin/bash

dt=$(date '+%d/%m/%Y %H:%M:%S');
echo "$dt"

Guess the problem is in 'echoing' to the csv.

  • 1
    if you need milliseconds, use date '+%F %T.%3N'. if you need microseconds, use date '+%F %T.%6N'. if you need nanoseconds, use date '+%F %T.%N'. %F is for date, it gives YYYY-MM-DD, you can use %D instead to get MM-DD-YYYY, %T is for time, HH:MM:SS. – computingfreak Mar 3 '17 at 1:17
4

This works:

#!/bin/bash
dt=`date '+%d/%m/%Y %H:%M:%S'`
echo "$dt"

It's also possible to use $( and ) instead of ` and `.

  • I forgot to mention it goes wrong after applying awk /home/pi/SB_tool/sunnybeamtool/bin/sunnybeamtool -l | awk -v tot=$tot -v dt=$dt -v day=$day -v mth=$mth ' following error 08/08/2013 23:03:03 awk: line 1: syntax error at or near : – Henry Aug 8 '13 at 21:02
  • Solved by asigning 2 separate values dt and tm. awk does not accept 1 variable with 2 values separated by space – Henry Aug 8 '13 at 21:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.