I have a bash script that will print yesterdays date. It looks like this:
#!/bin/bash
YESTERDAY_DATE=$(date '+%Y-%m-%d' -d '1 day ago')
echo $YESTERDAY_DATE
and I can run it like:
./script.sh
, output as follows:
2021-02-23
Now, I want to be able to override it at execution time so I went ahead and changed it to:
#!/bin/bash
: ${YESTERDAY_DATE=$(date '+%Y-%m-%d' -d '1 day ago')}
YESTERDAY_DATE_OVERRIDE=${$YESTERDAY_DATE:-(date '+%Y-%m-%d' -d '1 day ago')}
echo $YESTERDAY_DATE
and when I run it like:
YESTERDAY_DATE=2015-12-30 ./script.sh
, output as follows:
./script.sh: line 4: ${$YESTERDAY_DATE:-(date '+%Y-%m-%d' -d '1 day ago')}: bad substitution
2015-12-30
It does print the overridden date but with a bad substitution error. Is there a better way to do this?