Just started BASH scripting, and I tried to make a script 'args' to display all of the arguments that I give to it.
#!/bin/bash
if [ $# -eq 0 ]
then
echo "No arguments specified."
fi
val=
for ((i=1; i <= $# ; i++))
do
eval "\$val=\$$i"
echo "Argument number $i is $var."
done
However when I run 'args abc defgh' I get the following output:
What's wrong with the eval statement?
Hi,
this cannot work:
eval "\$val=\$$i"
Under bash it is the easiest to use:
var=${!i}
Or on other shells:
var=`eval echo \$$i`
HTH Chris
Here is one way of doing what you want to do.
for ((i=1; i <= $#; i++))
do
eval val='${'$i'}'
echo "Argument number $i: $val"
done
$ ./testcode one two three
Argument number 1: one
Argument number 2: two
Argument number 3: three
$
Thanks for your suggestions! I've got it working now!