-1

I'm trying to find a way the use a variable in this command to replace -10 with n_days var:

   n_days= -10
   date_prefix=$(date -d '-10 day' +%Y/%m/%d)

I tried this way but it didn't work:

   date_prefix=$(date -d '${n_days} day' +%Y/%m/%d)
Omar14
  • 1,827
  • 3
  • 18
  • 32
  • 2
    Change single quotes to double quotes. – bishop Aug 30 '18 at 12:33
  • Possible duplicate of [Why does a space in a variable assignment give an error in Bash?](https://stackoverflow.com/q/41748466/608639), [Expansion of variable inside single quotes in a command in Bash](https://stackoverflow.com/q/13799789/608639), [When to wrap quotes around a shell variable?](https://stackoverflow.com/q/10067266/608639), [Bash command substitution with a variable](https://stackoverflow.com/q/4935833/608639), etc. – jww Aug 30 '18 at 12:38

1 Answers1

3

Two things:

  1. Declare your variable properly (there is a space in your example)
  2. Use double quotes in place of single quotes to allow the variable to be interpolated

So:

n_days=-10
date_prefix=$(date -d "$n_days day" +%Y/%m/%d)
arco444
  • 20,737
  • 10
  • 61
  • 64