Simpler Navigation for Servers and Operating Systems
Completed: a much simpler Servers and Operating Systems section of the Community. We combined many of the older boards, so you won't have to click through so many levels to get at the information you need. Check the consolidated boards here as many sub-forums are now single boards.
Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

$# , ${#}, variable substituition question

SOLVED
Go to solution
Eric Lipede
Frequent Advisor

$# , ${#}, variable substituition question

Hi there
hope you guys had a good x-mas.
I have a small question (i hope).

A snippet of my current script is...

if [ $# -ge 2 ]
then
ARG_NUMBER="$#"
echo "$"$ARG_NUMBER""

it returns -> $4

...how do i get it to return the 4th argument (based on ARG_NUMBER) rather than the number of arguments without EXPLICITlLY saying echo $4 ?

Thks


Quod sum eris
4 REPLIES
Dennis Handly
Acclaimed Contributor
Solution

Re: $# , ${#}, variable substituition question

You need to use eval.
Or you can copy the parms to an array:
set -A save -- "$@"
echo ${save[4-1]}
Eric Lipede
Frequent Advisor

Re: $# , ${#}, variable substituition question

sounds good...ive only used eval on solaris and the only reference to it on my 11.23 box is /usr/lib/builtins/eval (and thats empty) ...is that a download job ?
Quod sum eris
Dennis Handly
Acclaimed Contributor

Re: $# , ${#}, variable substituition question

>is that a download job?

eval is a shell builtin. Try:
eval echo "$"$ARG_NUMBER
Highlighted
Eric Lipede
Frequent Advisor

Re: $# , ${#}, variable substituition question

gotcha - thks
Quod sum eris