-1

I use date \+%F\-%T to show date and time in this format: 2019-08-15-21:34:58.

From man date:

%F full date; same as %Y-%m-%d
%T time; same as %H:%M:%S

I recognized no explanation for \+ or \-.

I later understood that the backslashes are escaping, the plus is arguments prefix and the hyphen is just a string-like separation (date-time),
and also, that some use the simpler +'%F-%T' or +"%F-%T" instead.

Is there a reason to use + or - in date arguments?

  • 1
    What happens if you run the command without the backslashes? It produces the same output on my system (date 8.31). – Sparhawk Aug 15 '19 at 09:38
  • I remembered I tried it once and recall it worked the same. I understand it should work the same. –  Aug 15 '19 at 10:16

1 Answers1

6

The backslashes do nothing.

The - and + characters do not need to be escaped, because they are not special in any way in the syntax of any shell.

$ set -o xtrace
$ date +%F-%T
+ date +%F-%T
2019-08-15-11:51:46
$ date \+%F\-%T
+ date +%F-%T
2019-08-15-11:51:50

As you can see in the above trace output, the command that gets executed by the shell is the same regardless of the backslashes. Note too that the trace output is only "debugging output" from the shell and that whatever commands it outputs generally are not suitable for shell input.

The + introduces a date format specification. And the - is just a literal dash, part of the format specification (will be a dash in the output too).

The command date \+%F\-%T is the same as date +%F-%T. You will often also see the date format quoted, as in date +'%F-%T'. This makes no difference here as the format is a single word, but in date +'%F %T' it needs to be quoted to keep the shell from splitting the format on the space into two separate arguments to date.

What does makes a difference is the % characters. You would want to escape these if you use them in a crontab specification. See e.g. How can I execute `date` inside of a cron tab job?

The % character was special in the syntax of the fish shell prior to version 3.0 (for job expansions) but only when the first character of a word (so not here when after a +). Similarly, in zsh, a leading % character is special in that a ? following it is not taken as a glob operator (to allow things like kill %?cmd without having to quote the ?), but again that doesn't apply here.

Kusalananda
  • 333,661
  • For some reason though, Busybox's shell escapes the plus-sign when tab-completing. – ilkkachu Aug 15 '19 at 10:13
  • @ilkkachu Not for me in that shell. What are you completing? – Kusalananda Aug 15 '19 at 10:16
  • just filenames. Actually it seems to escape about everything that's not a-z0-9_-, so it's probably just being careful, dunno. – ilkkachu Aug 15 '19 at 10:25
  • Note that in the rc or akanga shells, backslash is not special, so date +%T would be different from date \+%T there (the latter being treated the same as date '\+%T' and so causing a syntax error by date) – Stéphane Chazelas Aug 15 '19 at 11:48
  • Note that some shells' xtrace do not output code which when reinput would produce the same outcome, and some could preserve the quoting from the input. So you may want to clarify that your test output there is run from bash or any other shell that behaves in a similar way. – Stéphane Chazelas Aug 15 '19 at 11:53
  • What I meant is that in several shells date +'%F %T' and date +%F %T for example produce the same xtrace output and yet those are two different date invocations. IOW, in general, it's not because the xtrace output shows the same thing that two commands are equivalent. – Stéphane Chazelas Aug 15 '19 at 13:42