Shell:将变量插入命令[duplicate]

时间:2022-12-05 23:53:06

This question already has an answer here:

这个问题在这里已有答案:

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

我试图找到一种方法,在此命令中使用变量用n_days var替换-10:

   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)

1 个解决方案

#1


3  

Two things:

  1. Declare your variable properly (there is a space in your example)
  2. 正确声明您的变量(示例中有一个空格)

  3. Use double quotes in place of single quotes to allow the variable to be interpolated
  4. 使用双引号代替单引号以允许插值变量

So:

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

#1


3  

Two things:

  1. Declare your variable properly (there is a space in your example)
  2. 正确声明您的变量(示例中有一个空格)

  3. Use double quotes in place of single quotes to allow the variable to be interpolated
  4. 使用双引号代替单引号以允许插值变量

So:

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