初学者:如何在花括号内使用$变量?

  • 本文关键字:变量 初学者 bash
  • 更新时间 :
  • 英文 :


我很难弄清楚如何在大括号内使用$var {}.

#!/bin/bash
read -p "How many files must there be built?" numb
for n in {1..$numb}
do
echo "Building file $n"
touch file$n.txt
done

这将导致:

How many files must there be built?8
Building file {1..8}

我试过将$numb括在反引号或双引号内,但无济于事。

您可以在循环中使用以下语法:

#!/bin/bash
read -p "How many files must there be built?" numb
for ((i=1; i<=$numb; i++)); do
echo "Building file $i"
touch file$i.txt
done

没那么花哨,但很好用。

最新更新