I am writing a script that compares the first 3 letters of every line (by using cut to get them) in a file to strings inside an Array. I have already looked around but the solutions i found didn't work on my system.Right now It looks like this:weekdays=([Mon]=1 [Tue]=1 [Wed]=1 [Thu]=1 [Fri]=1 [Sat]=1 [Sun]=1)input=/Foo/Bar.logwhile read -r linedocutline=$(echo ${line} | cut -c 1-3if [[ ${weekdays[$cutline]} ]]thenecho Matchelseecho No Matchfidone < ${input}The Line gets cut properly but something during the test returns a false Positive since no matter what the first 3 Letters are it returns a Match.When I checked the script with -x it showed me, instead of the actual test that it instead used  [[ -n 1 ]]And when I tested it with the [ ] expresion it showed a 1Does it check for every single char in the array and not just the whole words, or is there something else wrong with it?  If there is no problem, is there another way to compare the first 3 letters of a line to everything insde an array before continuing with the next?As a side note: I am indeed running Bash 4, so associative arrays should work
Testing if a String exists inside an Array of Strings
bash;shell script;aix
The basic error is that you aren't actually declaring an associative array:$ weekdays=([Mon]=1 [Tue]=1 [Wed]=1 [Thu]=1 [Fri]=1 [Sat]=1 [Sun]=1)$ echo ${weekdays[@]}1$ echo ${weekdays[0]}1$ echo ${weekdays[2]}$I'm not entirely sure how bash is dealing with it and why it just takes a 1, but I am sure that that is not an associative array. As explained in man bash (emphasis mine):An  indexed  array is created automatically if any variable is  assigned    to using the syntax name[subscript]=value.  The subscript  is treated as    an arithmetic expression that must evaluate to a  number.  To explicitly    declare an indexed array, use declare -a  name (see SHELL  BUILTIN  COM    MANDS  below).   declare  -a  name[subscript] is also accepted; the sub    script is ignored.Associative arrays are created using declare -A name.So, try this instead and it will work as you expect:declare -A weekdays=([Mon]=1 [Tue]=1 [Wed]=1 [Thu]=1 [Fri]=1 [Sat]=1 [Sun]=1)That said, your script is a bit more complex than you need. Here's a simpler version using the same approach:#!/bin/bashdeclare -A weekdays=([Mon]=1 [Tue]=1 [Wed]=1 [Thu]=1 [Fri]=1 [Sat]=1 [Sun]=1)input=/Foo/Bar.logcut -c 1-3 $input | while read -r line; do    if [[ ${weekdays[$line]} ]]    then            echo Match : $cutline : ${weekdays[$line]}    else            echo No Match    fidone    Although I would probably do it like this:#!/bin/bashcut -c 1-3 $1 | while read -r line; do        case $line in        Mon|Tue|Wed|Thu|Fri|Sat|Sun)                        echo yes;;                *)                        echo no;;        esacdoneThen, run the script with the target file name as an argument:script.sh /Foo/Bar.log