Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
bash - use a int variable as an index refering to position parameters passed to a function

I would like to use a variable as a index value to refer to positional variables passed to script, e.g.:

x=101
y=201
z=301

foo(){

    i=0;
    (( i+=1 )); # i=1
    echo "${$i}"; # parameter 1
    (( i+=1 )); # i=2
    echo "${$i}"; # parameter 2
    (( i+=1 )); # i=3
    echo "${$i}"; # parameter 3
}

foo x y z

output:

101
201
301

Arrays and loops will not help in this case, I need a simple method of incrementing a int variable and using this value as a index to retrieve a method parameter passed.

Advice?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I believe you need indirect variable expansion here:

x=101
y=201
z=301

foo() {
   local i
   local v
   for ((i=1; i<=3; i++ )); do
     v="${!i}" # will get x, y, z
     echo "${!v}" # will get values of $x, $y, $z
   done
}

Then call it as:

foo x y z

101
201
301
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!