In the script I am using the command
CURRENT_DATE_tmp=`date -d $CURRENT_DATE +%Y%m%d`.
It gives error date: invalid date `+%Y%m%d'
what may be the reason. I know that the variable CURRENT_DATE have value in it.
In the script I am using the command
CURRENT_DATE_tmp=`date -d $CURRENT_DATE +%Y%m%d`.
It gives error date: invalid date `+%Y%m%d'
what may be the reason. I know that the variable CURRENT_DATE have value in it.
This is happening because the variable is unset or empty, and you did not quote the variable:
If you use quotes, no error:
This has already been mentioned in a comment, but you must make sure there is actually a value in the variable $CURRENT if you want to do what you are trying to do. Try inserting these two lines in your script:
If one of the lines of output appears like the one below, then you have neglected to set
$CURRENT
to a non-empty string:Trying various variations of
date -d "$CURRENT" "+%Y%m%d"
(with or without quotes, with or without space after-d
) when$CURRENT
is an empty string, either I see the "invalid date" error or I get today's date.date -d $CURRENT_DATE
will print the date corresponding to the$CURRENT_DATE
variable.To store the date into a variable, try using
EDIT
To print an existing date into a new format, use
Better still, wrap the
$CURRENT_DATE
variable within quotes, so that dates with spaces don't break anything.In your current example, you have a space after the
-d
flag, remove it.