FirstName LastName
FirstName LastName

Reputation: 1911

Debian Bash Shell Variable Substitution error

I want to use a bash script to call other scripts

#!/bin/bash
./anotherScript $1 $2
./anotherScript $3 $4
#and so on

I don't know how many variables will be passed, so I am using a variable 'i' to run from 0 to $# and trying to get ${$i} as the argument. However Bash gives me a bad substitution error. I have tried the following:

a=1
echo $a
echo ${$a} #doesn't work
echo ${${a}} #doesn't work

None of them work. I am expecting ${$a} to evaluate to ${1} which should give me the first argument. I have looked through the man page and also the bash scripting guide on the Linux Documentation Project site

What am I doing wrong?

Upvotes: 0

Views: 148

Answers (3)

cforbish
cforbish

Reputation: 8819

I gave William Pursell an upvote. Another way to do this is to assign values to an array (which would be 0 relative):

list=("$@")
a=0
echo ${list[a]}
a=1
echo ${list[a]}

Upvotes: 1

Jonathan Leffler
Jonathan Leffler

Reputation: 753585

Another way to do it:

while [ $# -gt 0 ]
do
    ./anotherscript $1 $2
    shift 2
done

You can tune that to decide what happens with an odd number of arguments.

Upvotes: 2

William Pursell
William Pursell

Reputation: 212228

With bash, you can do an indirect reference with a !:

echo ${!a}

Upvotes: 4

Related Questions