13

I want to make the date command with nice formatting like this:

$ date +"%Y-%m-%d %H:%M:%S"
2015-09-17 16:51:58

But I want to save this in variable, so I could call from script like this: echo "$(nice_date) [WARNING] etc etc"

However it does not work

$ nice_date="date +%Y-%m-%d %H:%M:%S"
$ echo "$($nice_date)"
date: extra operand ‘%H:%M:%S’
Try 'date --help' for more information.

$ nice_date="date +\"%Y-%m-%d %H:%M:%S\""
$ echo "$($nice_date)"
date: extra operand ‘%H:%M:%S"’
Try 'date --help' for more information.

$ nice_date='date +"%Y-%m-%d %H:%M:%S"'
$ echo "$($nice_date)"
date: extra operand ‘%H:%M:%S"’
Try 'date --help' for more information.

What is correct way to do this, so that date command to get one correct argument?

migrated from serverfault.com Sep 18 '15 at 4:43

This question came from our site for system and network administrators.

  • As a side note, your date format string can be shortened to "%F %T" if you are using GNU date. – jordanm Sep 17 '15 at 14:14
  • The problem's the space. If you use nice_date="date +%Y-%m-%d-%H:%M:%S", it works. I can't currently see how to get that space protected from the second shell. – MadHatter Sep 17 '15 at 14:20
13

The reason your example fails is because of the way the shell's word splitting works. When you run "$($nice_date)", the shell is executing the date command with two arguments, "+%Y-%m-%d" and "%H:%M:%S". This fails because the format string for date must be a single argument.

The best way to do this is to use a function instead of storing the command in a variable:

format_date() {
  # echo is not needed
  date "+%Y-%m-%d %H:%M:%S" "$1"
}
format_date
format_date "2015-09-17 16:51:58"
echo "$(format_date) [WARNING] etc etc"

If you really wanted to store the command in a variable, you can use an array:

nice_date=(date "+%Y-%m-%d %H:%M:%S")
# again echo not needed
"${nice_date[@]}" "2015-09-17 16:51:58"

For more details on the complex cases of storing a command in a variable, see BashFAQ 050.

  • Thank you, but I am curious why my approach is not working, can you elaborate on that? – gilbertasm Sep 17 '15 at 14:14
  • @gilbertasm answer updated – jordanm Sep 17 '15 at 14:20
  • I'm getting date: extra operand ‘’ errors with both approaches above – Jeff Puckett Apr 24 '16 at 22:34
3

I agree a function is the best way to go. As an alternative simply store the format as a variable rather than the whole command:

$ nice_date='+%Y-%m-%d %H:%M:%S'
$ echo "$(date "$nice_date") [WARNING] etc etc"
  • 1
    @Kusalananda: Quite right. Thank you! Have updated the answer – Adam Apr 8 '18 at 18:55
  • And, um, sorry, but the echo and the command substitution are also superfluous. – Kusalananda Apr 8 '18 at 19:08
  • 1
    Yes, they were technically superfluous in the cut-down example I used (I could argue they produce more readable code but that's a question of taste). Have updated to use the example from the OP, in which case they are needed – Adam Apr 8 '18 at 19:12
  • Yes, that example makes more sense. – Kusalananda Apr 8 '18 at 19:19
1

Shortest answer is

#if you want to store in a variable
now=$(date '+%F" "%T');
echo $now

#or direct output (single quote may not needed)
date '+%F" "%T'
0

Please try out the below:

You need to add the symbol "`" the symbol that is on the tilde key in the keyboard. The symbol says the shell to execute the command first and then assign to the variable.

[root@RHEL01 ~]# My_Var=`date +"%Y-%m-%d %H:%M:%S"`
[root@RHEL01 ~]# echo $My_Var
2015-09-17 10:05:30
[root@RHEL01 ~]#
  • Same problem as Iain's answer. – MadHatter Sep 17 '15 at 14:09
0

Instead of:

nice_date="date +%Y-%m-%d %H:%M:%S"
echo "$($nice_date)"

Try:

nice_date_format="+%Y-%m-%d %H:%M:%S"
echo $(date "$nice_date_format")

Also, you don't need the last command substitution:

date "$nice_date_format"
-1

Had the same problem; had to put a date into an awk print for processing by splunk. I used

DATE1=`date +%Y-%m-%d`

DATE2=`date +%T`

then

awk ' { print "['$DATE1' '$DATE2'] topic=" $1, etc
  • 1
    Can you explain exactly how this answers the question? – G-Man Feb 14 at 21:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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