问题描述
COMMANDS ='ls /df〜du - hs〜/ Devel /'
for $ in $ COMMANDS;做
echo $ i
完成
如果结果是: p>
ls /
df〜
du -hs〜/ Devel /
但我找不到空格的正确语法。
COMMANDS =(ls /df〜du -hs〜/ Devel /)
为$ {COMMANDS [ @]};做
echo$ i
完成
这使用一个数组来存储命令。此功能也可以在 I would like to do something like this: Where the result would be: But I can't find the right syntax for the spaces. This uses an array to store the commands. This feature is also available in Arrays behave like the 这篇关于用空格打开循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! ksh
, zsh
中使用,但不在 sh $ c中$ c
$ b $数组的行为就像$ @
参数数组。在$ {ARRAY_NAME [@]}
(引号很重要)上应用 for
项目相继。如果你省略了引号,它们将会一起被分散在你的 IFS
环境变量('\t'
,'\ n'
和''
默认)。 b $ bCOMMANDS='"ls /" "df ~" "du -hs ~/Devel/"'
for i in $COMMANDS; do
echo $i
done
ls /
df ~
du -hs ~/Devel/
COMMANDS=("ls /" "df ~" "du -hs ~/Devel/")
for i in "${COMMANDS[@]}"; do
echo "$i"
done
ksh
, zsh
, but not in sh
."$@"
argument array. Applying a for
loop on "${ARRAY_NAME[@]}"
(the quotes are important) will give you each item in succession. If you omit the quotes, it'll all get smushed together and split on the separators present in your IFS
environment variable ('\t'
, '\n'
and ' '
by default).