在Bash中,可以使用以下方法向数组中添加元素以便以后打印它们:
my_array=()
my_array[0]="element1"
my_array[1]="element2"
my_array=()
my_array+=("element1")
my_array+=("element2")
my_array=()
my_array+=("element1" "element2")
my_array=()
another_array=("element1" "element2")
my_array+=("${another_array[@]}")
在以后打印数组元素时,可以使用以下方法:
echo "${my_array[*]}"
echo "${my_array[@]}"
for element in "${my_array[@]}"; do
echo "$element"
done
以上是向Bash数组中添加元素以在以后打印它们的方法。对于更多关于Bash数组的操作和用法,可以参考腾讯云的Shell脚本开发指南:https://cloud.tencent.com/document/product/213/2176
领取专属 10元无门槛券
手把手带您无忧上云