shell脚本中变量无法读取空格的解决方法

有一天遇到需要实现shell脚本中变量连着括号一起读取,实现方法如下:

如果不加双引号会翻车(你可以尝试去除不同的双引号看看效果)

#!/bin/bash
OLDIFS=IFS
#IFS=`echo -e "\n"`
f1="DAFS DFFS"
f2=2
f3=3
f4=4
array=("f1" $f2 $f3 f4)
for element in "{array[@]}"
do
echo $element

Reference:

Licensed under CC BY-NC-SA 4.0
Built with Hugo
Theme Stack designed by Jimmy