echo 命令没有给出正确的输出

echo 命令没有给出正确的输出

我尝试创建类似12DEC2013bhav.csv.zip以下脚本的输出。

但它给了我类似的东西12DEC.csv.zip

for (( i = 2013; i <= 2014; i++ ))
do
    for m in JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
    do
        for (( d = 1; d <= 31; d++))
        do
            echo "$d$m$ibhav.csv.zip"
        done
    done
done

我该如何改正它?

答案1

问题是您正在尝试引用一个名为$ibhav(not $i) 的变量。

变量可以包含多个字符,在您的示例中,shell 无法判断您的意思是$i$ibhav(或$ibha$ibh$ib)。

解决方法是将变量名括起来:

echo "${d}${m}${i}bhav.csv.zip"

这样就明确了您引用的是哪个变量。


man bash

   Parameter Expansion
        The `$' character introduces parameter expansion, command substitution, or
        arithmetic expansion.  The parameter name or symbol to be expanded may  be
        enclosed  in  braces, which are optional but serve to protect the variable
        to be expanded from characters immediately following  it  which  could  be
        interpreted as part of the name.

        When  braces  are  used,  the  matching  ending brace is the first `}' not
        escaped by a backslash or within a quoted string, and not within an embed‐
        ded arithmetic expansion, command substitution, or parameter expansion.

        ${parameter}
               The  value  of  parameter  is substituted.  The braces are required
               when parameter is a positional parameter with more than one  digit,
               or  when  parameter  is  followed by a character which is not to be
               interpreted as part of its name.  The parameter is a shell  parame‐
               ter as described above PARAMETERS) or an array reference (Arrays).

相关内容