66

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.

manatwork
  • 30,549
  • 7
  • 101
  • 91
Henry
  • 827
  • 1
  • 7
  • 4
  • 6
    “_” and “-” are the only characters accepted by _who_? What stops you replacing the “_” with “ ” in `date`'s format string? – manatwork Aug 08 '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 04 '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 Says 'Reinstate Monica' Jan 19 '19 at 02:20

1 Answers1

109
#!/bin/bash

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

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

user1293137
  • 1,692
  • 1
  • 11
  • 5
  • 4
    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 03 '17 at 01:17
  • `echo $dt` is also ok – pcko1 Mar 02 '21 at 08:20