I'm trying to cycle through an unknown number of arguments given to a bash script (or function within), to effectively handle the odds ($1
, $3
, etc.) and evens ($2
, $4
, etc.) in different ways.
I know I can get the number of arguments using $#
, and the arguments themselves using $@
, and of course echo $1 $2
or printf '%s\n' "$1"
both work. What I'm needing to do is effectively echo the odds echo $1 $3 $5 ...
with an unknown number, then separately deal with the evens, and count the characters of these individually also, so need to get these programmatically if at all possible.
Note: Some of the input will have spaces, but where will always have quotes. An example would be 1 "This one" "Another one" "and another" "last one"
.
I've tried (these are just to get output for brevity):
Putting $@
into an array of itself, in both for and while arrangements (understanding this zero indexes the array):
indexedarray="$@"
for i in {0..$#..2}; do #This in itself creates an error ({0..5..2}: syntax error: operand expected (error token is "{0..5..2}")).
echo -n "${indexedarray[$i]}
done
This produces empty output:
i=0
while [ $i -lt $# ]; do
echo ${INDEXEDARRAY[i]}
((i+2))
done
And the (somewhat) obvious foibles inside of for or while loops:
echo "${$@[$i]}"
echo $"${i}"
None of which work.
Any ideas on how I might improve this, and get the output I need?