I would like to use a variable say:
i=1
as a value to refer to positional variables passed to script, e.g.:
x=101
y=201
z=301
foo(){
echo "$1"
echo "$2"
echo "$3"
}
foo x y z
output:
101
201
301
Instead of refering to each parameter by index, how could I use i to increment through as a index variable?
To clarify:
foo() {
local i=1
echo "$i" #echo first paramter
(( i+=1 ))
echo "$i" #echo second parameter
#etc.
}
what is the syntax for the echo "$i" part?
UPDATE after @Eric answer
~$ t=5
~$ foo() { i=1; echo "${!i}"; }
~$ foo t
t
~$
Update #2
So in short, the only way I can make my method work is by this:
foo() { #assuming 3 parameters
i=0
(( i+=1 ))
var="${!i}"
echo "${!var}"
(( i+=1 ))
var="${!i}"
echo "${!var}"
(( i+=1 ))
var="${!i}"
echo "${!var}"
}
${!i}can be useful for youshiftcommand to operate argumentsiyourself, but more or less. What is it that you're really trying to accomplish though? This feels like it could be a bit of an X-Y Problem