0

I have the following bash code:

for (( i=4; i<=$var; ))
do
    temp=`echo $i`
done

I need to assign the command line argument stored in $4 to the variable temp which is not happening.

4
  • 3
    Not sure what you're asking but it seems like you want temp=$4. Commented Apr 7, 2014 at 18:13
  • Prior to adding an echo you should put all your commands in the variable then echo the information Commented Apr 7, 2014 at 18:16
  • 1
    Why would you use a loop if you only need to work with $4? Commented Apr 7, 2014 at 18:20
  • 1
    i have a feeling what he really wants is to loop through all the command line args Commented Apr 7, 2014 at 18:24

1 Answer 1

1

Given a variable i=4 you can get the value of $4 using ${!i}:

set -- foo bar baz thisOne etc
i=4
echo "${!i}"  

This prints the fourth positional parameter, thisOne.

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

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.