0

Is it possible to do something like this

#!/bin/bash 
noOfParameters=2
paramname1="test1"
paramname2="test2"
i=0
while [ $i -ne ${noOfParameters} ]
  do 
     i=`expr $i + 1`
     echo ${paramname$i}
 done

I am trying to achieve output as

test1
test2

I am getting "main.sh: line 10: ${paramname$i}: bad substitution" error

2

2 Answers 2

1

I'd personally do this using an array

arr[0]="test"
arr[1]="test2"

for item in "${arr[@]}"
do
  printf "%s\n" "$item"
done

Sign up to request clarification or add additional context in comments.

Comments

0

You can use indirect references like

for ((i=1;i<3;i++)); do
   varname=paramname$i
   echo "${!varname}"
done

Perhaps you can bypass your problems with something like

set | grep -E "paramname[12]=" | cut -d"=" -f2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.