eval

num=4
mkdir data_{1..$num}

居然得到的是data_{1..4}这个目录名1)

发帖求助,得知:

num=4; eval mkdir data_{1..$num}

如果for循环内引入变量,应该这样写:

START=1
END=$num
for i in $(eval echo "{$START..$END}");do
	echo $i
	[[ ! -d /DATA_$i ]] && mkdir -p /DATA_$i
	echo "创建挂载点成功."
done

原来只知道这种用法:

eval `dircolors`

#花括号(brace)的展开在变量(variable)的展开之前, 所以你需要 eval . 
man bash
The order of expansions is: brace expansion, tilde expansion, parameter, variable and arithmetic expansion and command substitution (done in a left-to-right fashion), word splitting, and pathname expansion. 

1)
把1..4当成字符串了
  • linux/shell/eval.txt
  • 最后更改: 2019/04/16 18:31
  • (外部编辑)