I am trying to use nested strings, but have variables at both stages (outer and inner strings). My understanding is that variables can only be called within double quotes, so I can't use single quotes inside of double as I would typically nest strings, so I'm stuck.
Specifically, I want to pass a single number option into a bash file, and then use the formatted date. So I'll call:
./mycode.sh 4
I then need a nice formatted string variable for the date 4 days ago, something like this:
DATE="$(date +%Y-%m-%d --date'=-$1 days')"
(this works perfectly with a hard-coded digit in place of $1
). This seems to sort of 'escape' the inner dollar sign, and I get the error invalid date '$1'
.
But weirdly, as I test a simpler example, the inner quotes don't always seem to break the $. For instance, this works as expected:
OFFSET="'-$1 days'"
echo $OFFSET
However, I get errors when I try to use the new OFFSET string in the above date command. So I'm basically just confused about a) how to solve my issue above, and b) under what situations does the dollar sign actually invoke the variable?