0

I am learning shell sripting and got stuck. here is the code #!/bin/bash

a=0
myarray[$a]=$1
echo $myarray[$a]
((a+=1))
echo $a

Output:

#./varcheck sa
sa[0]
1

somebody please tell me why the name of array is getting replaced with argument that I want to assign to 0th index of array.

1

1 Answer 1

4
echo "${myarray[$a]}"

is how you output an array member. Alternatively

echo "${myarray[a]}"

since the index is guaranteed to be arithmetic context unless you're using associative arrays. Thus, you could actually remove a line:

a=0
myarray[a]=$1
echo "${myarray[a++]}" # Get element at zeroth index
echo $a # Get post-incremented expansion of a.
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.