So what is the date command corresponding to var="$(date +"%x %r %Z")"?
Well as already stated in another answer you missed to quote the options right, it should be at least:
date +"%x %r %Z"
This is needed because you are using white-spaces to keep the different information apart, the man page for date suggests using -
, or _
, with this you could omit the quoting all together in normal terminal, or you escape the white-spaces.
Examples:
using '-' as divider
date +%x-%r%Z
gives the following output:
21.09.2018-11:30:15 CEST
using '_' as divider
date +%x_%r%Z
gives the following output:
21.09.2018_11:30:15 CEST
using escaped white-spaces as divider
date +%x\ %r%Z
gives the following output:
21.09.2018 11:30:15 CEST
using no dividers at all
date +%x%r%Z
gives the following output:
21.09.201811:30:15 CEST
Remainder for a special use case. When you try to incorporate date into a crontab
command, you need to escape the %
sign too. So as undivided information the command would look like this (Before mentioned rules apply for escaping still):
date +\%x\%r\%Z
date +"%x %r %Z"
with the quotes on it so that it knows that all the % are part of it. – Terrance Sep 21 '18 at 5:14