Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question

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
up vote 10 down vote accepted

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.

share|improve this answer
    
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 II Apr 24 at 22:34

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

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)
share|improve this answer

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"
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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