0

I have stored some strings in array1, lets say

array1[0]=apple
array2[1]=orange

and array2 contains

array2[0]=apple
array2[1]=mango

I want to loop through each element and check if they match. I tried using this condition inside loop but it didnot work

if [ "$array[i]" = "$array2[j]" ]
1
  • I don't see a loop in your posting. Complementing the answer given by Socowi: If you prefer the array access syntax you are using, you could consider switching to zsh (which also would make the quotes unnecessary). Bear in mind that array indexing in zsh starts with 1. Commented Apr 7, 2021 at 10:12

1 Answer 1

1

To access the elements of an array in bash you have to use ${array[i]} instead of just $array[i]. Because the [ cannot normally be part of a variable name, bash interprets $array[i] as ${array} followed by a literal [i].

By the way: https://www.shellcheck.net/ would have found this error.

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

1 Comment

thanks, the problem was in syntax, it solved my doubt.

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.