如何循环在bash脚本参数 [英] How to iterate over arguments in bash script
本文介绍了如何循环在bash脚本参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个复杂的命令,我想做出一个shell / bash脚本。我可以在 $ 1
来讲很容易写:
I have a complex command that I'd like to make a shell/bash script of. I can write it in terms of $1
easily:
foo $1 args -o $1.ext
我希望能够将多个输入名称传递到脚本。
I want to be able to pass multiple input names into the script.
什么是做到这一点的正确方法?
What's the right way to do this?
和,我当然要处理与空格的文件名在其中。
And, of course I want to handle filenames with spaces in them.
推荐答案
使用$ @
重新present所有的参数:
Use "$@"
to represent all the arguments:
for var in "$@"
do
echo "$var"
done
这将每个参数迭代,并打印出来在单独一行。 $ @的行为就像$ *除了引用时的参数被分解正确,如果有空格的:
This will iterate over each argument and print it out on a separate line. $@ behaves like $* except that when quoted the arguments are broken up properly if there are spaces in them:
sh test.sh 1 2 '3 4'
1
2
3 4
这篇关于如何循环在bash脚本参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文