1

I've given a variable which hold a number

daysAgo=1

I would like to expand this variable in a get date expression. Like this:

$(date +%d -d '$daysAgo days ago')

What do I need to do that the $daysAgo variable gets expanded?

I tried like that without success:

daysAgo=1
exp="'${daysAgo} days ago'"
$(date +%d -d $exp)

2 Answers 2

1

Just use ", not '. Double quotes allows expansion of variables within the quotes, single quotes don’t.

daysAgo=1
echo $(date +%d -d "$daysAgo day ago")
06

daysAgo=1
exp="$daysAgo days ago"
echo $(date +%d -d "$exp")
06
1

You need to use " instead of '. From man bash:

Enclosing characters in single quotes preserves the literal value of each character within the quotes.

You must log in to answer this question.

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