# check the difference between 2 array

**URL:** <https://community.unix.com/t/check-the-difference-between-2-array/303513>\
**Category:** Shell Programming and Scripting\
**Created:** [January 27, 2012, 1:10pm UTC](https://community.unix.com/t/check-the-difference-between-2-array/303513 "2012-01-27T13:10:05Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![amlife](https://community.unix.com/letter_avatar/amlife/32/5_5575768a8748004e209b776fc1b2916d.png) [@amlife](https://community.unix.com/u/amlife)\
**Post date:** [January 27, 2012, 1:10pm UTC](https://community.unix.com/t/check-the-difference-between-2-array/303513/1 "2012-01-27T13:10:05Z")

</div>

Hello

Thanks everyone for the help earlier, what I would like to learn now is how can I achieve the following :

array1 = (1234567,7665456,998889,000909)  
array2 = (1234567,5581445,998889,000909)

Result

5581445 doesn't exist on array1

Thank you

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [January 27, 2012, 2:37pm UTC](https://community.unix.com/t/check-the-difference-between-2-array/303513/2 "2012-01-27T14:37:42Z")

</div>

Please show us what you have done so far.

You should have the logic and ask for help on which commands to use.

---

<div class="post-metadata">

**Author:** ![cfajohnson](https://community.unix.com/user_avatar/community.unix.com/cfajohnson/32/16669_2.png) [@cfajohnson](https://community.unix.com/u/cfajohnson)\
**Post date:** [January 27, 2012, 4:53pm UTC](https://community.unix.com/t/check-the-difference-between-2-array/303513/3 "2012-01-27T16:53:32Z")

</div>

Usng bash:

```nohighlight
array1=(1234567 7665456 998889 000909)
array2=(1234567 5581445 998889 000909)

grep -v -f <(printf "%s\n" "${array1[@]}") <(printf "%s\n" "${array2[@]}")

```

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [January 30, 2012, 10:33am UTC](https://community.unix.com/t/check-the-difference-between-2-array/303513/4 "2012-01-30T10:33:07Z")

</div>

> [@cfajohnson](#):
>
> Usng bash:
> 
> ```plaintext
> array1=(1234567 7665456 998889 000909)
> array2=(1234567 5581445 998889 000909)
> grep -v -f <(printf "%s\n" "${array1[@]}") <(printf "%s\n" "${array2[@]}")
> 
> ```

Ran above code using bash and received the following error:

```plaintext
grep: /dev/fd/62: No such file or directory

```

The following code works:

```plaintext
array1=(1234567 7665456 998889 000909)
array2=(1234567 5581445 998889 000909)
printf "%s\n" "${array2[@]}" | grep -v "$(printf "%s\n" "${array1[@]}")"

```

---

<div class="post-metadata">

**Author:** ![ahamed101](https://community.unix.com/user_avatar/community.unix.com/ahamed101/32/1118_2.png) [@ahamed101](https://community.unix.com/u/ahamed101)\
**Post date:** [January 30, 2012, 10:36am UTC](https://community.unix.com/t/check-the-difference-between-2-array/303513/5 "2012-01-30T10:36:54Z")

</div>

> [@shell\_life](#):
>
> Ran above code using bash and received the following error:
> 
> ```plaintext
> grep: /dev/fd/62: No such file or directory
> 
> ```
> 
> The following code works:

It worked for me though!

```plaintext
root@bt> bash --version
GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu)

root@bt> array1=(1234567 7665456 998889 000909)
root@bt> array2=(1234567 5581445 998889 000909)
root@bt> grep -v -f <(printf "%s\n" "${array1[@]}") <(printf "%s\n" "${array2[@]}")
5581445

```

--ahamed

---

<div class="post-metadata">

**Author:** ![Shell\_Life](https://community.unix.com/user_avatar/community.unix.com/shell_life/32/521_2.png) [@Shell\_Life](https://community.unix.com/u/Shell_Life)\
**Post date:** [January 30, 2012, 10:45am UTC](https://community.unix.com/t/check-the-difference-between-2-array/303513/6 "2012-01-30T10:45:28Z")

</div>

I used:

```nohighlight
GNU bash, version 3.2.0(1)-release (x86_64-unknown-linux-gnu)

```

As much as possible, if one solution can be given in a more universal usage, then we must provide it.

Otherwise, for the same requirement, there will be several possible solutions, based on which OP (linux, unix), shell, shell version, etc.

Let's be generic as much as we can.

---

<div class="post-metadata">

**Author:** ![drl](https://community.unix.com/user_avatar/community.unix.com/drl/32/542_2.png) [@drl](https://community.unix.com/u/drl)\
**Post date:** [January 30, 2012, 11:05am UTC](https://community.unix.com/t/check-the-difference-between-2-array/303513/7 "2012-01-30T11:05:32Z")

</div>

Hi.

See also [check the difference between 2 arrays](http://www.linuxforums.org/forum/programming-scripting/186374-check-difference-between-2-arrays.html)

Best wishes ... cheers, drl
