I want to use a variable inside date command . GNU date is not supportable in my system. I use Sunos and when I use date with -d option it is shwoing -d as bad substitution.

RELEASE_DATE_MINUS_2=`date -"${RELEASE_DATE: -2} ${RELEASE_DATE:4:3} ${RELEASE_DATE:0:4} - 2 days" '+%Y-%m-%d'`
echo "Release is $RELEASE_DATE" 
echo "release_date-2 is $RELEASE_DATE_MINUS_2

I have tried the above code snippet. I should pass the actual release_date which i get it form another .cnf file like 28OCT2018 with this input I should get two days before that date like 2018-10-26.

  • 1
    What language is rel_date a variable in?
    – jesse_b
    Commented Jan 7, 2019 at 13:56
  • Will your input date always be in the same format? Also will it pad day numbers less than 10? ie 2019JAN07 or 2019JAN7?
    – jesse_b
    Commented Jan 7, 2019 at 14:03
  • What OS and/or Distribution is this running on? (Do you have GNU date?) Commented Jan 7, 2019 at 14:08
  • 3
    "I have used various date command features" care to share which ones you've tried, and how? Or do we need to guess? Commented Jan 7, 2019 at 14:08
  • 2
    "I am getting the error in stating -d option is not a valid argument." What command did you use, and what was the exact error message? (Don't approximate.) Commented Jan 7, 2019 at 14:09

4 Answers 4


With the zsh shell:

zmodload zsh/datetime
strftime -rs d %Y%b%d.%H $rel_date.12 &&
  strftime -s two_days_before %Y-%m-%d $((d - 86400*2)) || exit

echo $two_days_before

Perl can be useful for this:

perl -MTime::Piece -slE 'say +(Time::Piece->strptime($date, "%Y%b%d") - 2 * 86_400)->ymd' -- -date="$rel_date"

You specify the format of the incoming date variable to parse it into a time object, do the date arithmetic, then output into the desired YYYYY-mm-dd format.

Documented here: https://perldoc.perl.org/Time/Piece.html


You tagged this with shell so I will assume the variable is supposed to be in shell but FYI you cannot have spaces around the = for a shell variable.

GNU date will not accept your input date in that format but will accept it like 28 OCT 2018 so using shell parameter expansion we can rearrange the date (as long as it will always be in the same format):

date -d "${rel_date: -2} ${rel_date:4:3} ${rel_date:0:4}"

Now in order to subtract 2 days we simply need to add:

date -d "${rel_date: -2} ${rel_date:4:3} ${rel_date:0:4} - 2 days" 

And to have it output in your requested format

date -d "${rel_date: -2} ${rel_date:4:3} ${rel_date:0:4} - 2 days" '+%Y-%m-%d'
  • We don't (yet) know for sure the OP is using GNU date. Commented Jan 7, 2019 at 14:12
  • We don't but I have provided a GNU date solution.
    – jesse_b
    Commented Jan 7, 2019 at 14:12
  • Hi Jess, Thanks for your help. But even this seems to be not working .
    – Sandra
    Commented Jan 8, 2019 at 14:18
  • It just tells the same bad substitution error . Below is my code snipet.
    – Sandra
    Commented Jan 8, 2019 at 14:18
  • #!/usr/bin/ksh RELEASE_DATE="28 OCT 2018" RELEASE_DATE_MINUS_2=date -"${RELEASE_DATE: -2} ${RELEASE_DATE:4:3} ${RELEASE_DATE:0:4} - 2 days" '+%Y-%m-%d' echo "Release is $RELEASE_DATE" echo "releasE_date-2 is $RELEASE_DATE_MINUS_2"
    – Sandra
    Commented Jan 8, 2019 at 14:19

It could be useful to know which ksh{88,93} are you using.

According to ksh93(1) you can find something about date:

 printf format [ arg ... ]

--practical example

: printf "%(%d.%m,%Y)T\n"             
: printf "%(%d.%m.%Y)T\n" "2 days ago"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .