17

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?

2
  • 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
16

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" "$@"
}
format_date
format_date -d "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[@]}" -d "2015-09-17 16:51:58"

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

7
  • 2
    I'm getting date: extra operand ‘’ errors with both approaches above Apr 24 '16 at 22:34
  • This is not working ... bash date command is the most confused thing to handle.
    – MaXi32
    Sep 5 at 11:20
  • date: extra operand ‘2015-09-17 16:51:58’ Try 'date --help' for more information.
    – MaXi32
    Sep 5 at 11:21
  • @MaXi32 i've only tested this solution with GNU date. It might not work with other versions such as on osx.
    – jordanm
    Sep 5 at 15:59
  • 1
    @MaXi32 I was able to reproduce it on my current fedora machine. Updated the answer, the issue was a missing -d option for date.
    – jordanm
    Sep 6 at 21:06
4

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"
0
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'
1

One easy way is to just to 'escape' the space with a \

nice_date="date +%Y-%m-%d\ %H:%M:%S"
1
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

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 ~]#
1
  • Same problem as Iain's answer.
    – MadHatter
    Sep 17 '15 at 14:09
-2

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

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.