comparing two arrays or strings in bash Hi there, im having issue with comparing two variables, in a bash script. im trying to do the following: - get a word from user 1 - split the word into array - get a character from user2 trying to compare the character entered by user 2 with every single character in the array entered by user1. In this article, we’ll cover the Bash arrays, and explain how to use them in your Bash scripts. Let's say: array1 contains values 1 and 2 array2 contains values 2 and 3. I have one array that contains the results of some search and if the script has found all the items, then it should stop, so my idea is to have a while loop à la: They end up passed as strings. The lists need to be sorted, but that's easy to achieve. Keep in mind that bash arrays are sparse, meaning they can contain empty values at various locations. If you agree with that, then you probably won't want to read about the "new" associative arrays that were added in version 4.0 of bash. Bash Associative Array (dictionaries, hash table, or key/value pair) You cannot create an associative array on the fly in Bash. You can think of an array is a variable that can store multiple variables within it. The code first reads all pathnames from one of the files into an array, files1, using mapfile. Also, arrays cannot be passed as arguments. Answer . Comparing arrays in bash Is there a way to compare an array in a while conditions? Create indexed arrays on the fly bash how to echo array. Bash Arrays # Bash supports one-dimensional numerically indexed and associative arrays types. Array intersection in bash, How do you compare two arrays in bash to find all intersecting values? printf "%s\n" "${mydata[@]}" | grep "^${val}$" You can also get the index of an entry with grep -n, which returns the line number of a match (remember to subtract 1 to get zero-based index) This will be reasonably quick except for very large arrays. It is assumed that no pathname in the two input files contain any embedded newlines. Arrays are one of the most used and fundamental data structures. You can see if an entry is present by piping the contents of the array to grep. Hi, I am quite scripting illiterate and have been trying to write a bash script to compare to two files which i have populated in two seperate arrays as below and … The bash man page has long had the following bug listed: "It's too big and too slow" (at the very bottom of the man page). I comm(1) is a tool that compares two lists and can give you the intersection or difference between two lists. I would reach for awk here. Associative arrays can be created in the same way: the only thing we need to change is the option used: instead of lowercase -a we must use the -A option of the declare command: $ declare -A my_array This, as already said, it's the only way to create associative arrays in bash. You can only use the declare built-in command with the uppercase “-A” option.The += operator allows you to append one or multiple key/value to an associative Bash array. You can define three elements array (there are no space between name of array variable, equal symbol and starting bracket): FILES=(report.jpg status.txt scan.jpg) This command will write each element in array: echo ${FILES[*]} Index in shell arrays starts from 0. Here, the result is produced on both the terminal and in the file file-comparison.out. In my earlier article I gave you an overview and difference between strings and integers in bash.Now there are different comparison operators which can be used to compare variables containing numbers, which we will learn in this tutorial guide. So, array comparison in bash is going to be a not-so-fun exercise.