0

i have command like this

date -d @$(date -d 'Sat, 08 Aug 2020 00:00:00' "+%s")  +'%Y-%m-%d'

which output this

2020-08-08

So i want to use thins in bash scrip, i created this (this is just part of realy big script)

Date1=$1
date -d @$(date -d $Date1 "+%s")  +'%Y-%m-%d'

But when i try to run like this

./test.sh "Sat, 08 Aug 2020 00:00:00"

I get

date: extra operand ‘Aug’
Try 'date --help' for more information.
date: invalid date ‘@’

So its look like that "" desapear when passing argument

  • You need to quote the $Date1. Otherwise, it will be expanded as date -d Sat, 08 Aug 2020 00:00:00 "+%s", which is not what you want. – annahri Oct 09 '20 at 08:58

1 Answers1

1

Wrap your $Date1 inside the quotes and it should work:

date -d @$(date -d "$Date1" "+%s")  +'%Y-%m-%d'

See also When is double-quoting necessary?

Kusalananda
  • 333,661
z.h.
  • 994