bcd
bcd

Reputation: 454

Comparing an associative array

I have an associative array that holds filenames. I'd like to use cmp on them to see if they differ from each other.

declare -A configfiles
configfiles["file1"]="file2"

for k in "${!configfiles[@]}"
    do
        if cmp $k $configfiles[$k]; then
            echo Do something
        fi
    done

Bash returns: cmp: [file1]: No such file or directory

How can I get bash to omit the brackets while calling cmp?

Upvotes: 0

Views: 150

Answers (1)

chepner
chepner

Reputation: 531055

You need to use the correct syntax for accessing array elements:

if cmp "$k" "${configfiles[$k]}"; then

Upvotes: 1

Related Questions