重命名文件名中带有下划线的文件时shell出现意外行为

重命名文件名中带有下划线的文件时shell出现意外行为,shell,Shell,此命令将按预期生成十个文件: for i in `seq 10`; do touch model; mv model model_$i; done 但是,此文件将只生成一个名为“model_”的文件: 文件名中的下划线似乎有一些影响,但实际发生了什么?感谢您的回答。在您的第二个示例中,$i_best被解释为变量名,并可能扩展为空字符串 使用大括号和/或引号展开$i: for i in `seq 10`; do touch model_best # just quo

此命令将按预期生成十个文件:

for i in `seq 10`; do touch model; mv model model_$i; done
但是,此文件将只生成一个名为“model_”的文件:


文件名中的下划线似乎有一些影响,但实际发生了什么?感谢您的回答。

在您的第二个示例中,
$i_best
被解释为变量名,并可能扩展为空字符串

使用大括号和/或引号展开
$i

for i in `seq 10`; do 
    touch model_best     

    # just quote the variable (my favourite)
    mv model_best model_"$i"_best      

    # or (belt and braces approach, quotes can also go around the whole arg)
    # mv model_best model_"${i}"_best

    # or just curly braces
    # fine here since $i contains no spaces or glob characters 
    # (but I don't like it)
    # mv model_best model_${i}_best
done

谢谢你的解释。
for i in `seq 10`; do 
    touch model_best     

    # just quote the variable (my favourite)
    mv model_best model_"$i"_best      

    # or (belt and braces approach, quotes can also go around the whole arg)
    # mv model_best model_"${i}"_best

    # or just curly braces
    # fine here since $i contains no spaces or glob characters 
    # (but I don't like it)
    # mv model_best model_${i}_best
done