How to iterate through part of the positional arguments in a command procedure in bash?

StackOverflow https://stackoverflow.com/questions/23471617

Вопрос

I have written a command procedure that takes 12 positional arguments. How can i iterate only through the first 10 (for example). If i use shift [n], then argument $0 comes in but I don't want to use it. Can I use something like list comprehension?:

for arg in [$1..$10]
do 
   echo $arg
done
Это было полезно?

Решение

Use parameter expansion like so:

for i in "${@:1:10}"; do
   echo "$i"
done

For more info, see man bash -> Parameter Expansion -> Substring Expansion

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top