Incremental value of a variable in a bash function

Therefore, I can almost guarantee that this is a stupid question, but I just cannot understand it. I am trying to count how many times I have indexed files. I need to increment the counter every time I find a PDF file that matches certain criteria (metadata must contain 3 specific values). The variable in question is indexCount, and I highlighted the line where I am trying to increase it with #NOT SURE ABOUT THIS LINE

index() {
    for file in *
    do
        [ -d "$file" ] && (cd "$file"; index)
        oldPath=$(pwd)
        if [ "$( echo "$file" | grep -E '.*\.pdf' )" ]; then
            metadata="$(pdftk "$file" dump_data)"

            echo "$metadata" | $(grep -e '^InfoKey: Title' >/dev/null 2>&1) && echo "$metadata" | $(grep -e '^InfoKey: Author' >/dev/null 2>&1) && echo "$metadata" | $(grep -e '^InfoKey: CreationDate' >/dev/null 2>&1)
            if [ $? -eq 0 ]; then
                path="$(pwd)/""$file"
                title=$(getAttr "$metadata" '^InfoKey: Title')
                author=$(getAttr "$metadata" '^InfoKey: Author')
                creation=$(getAttr "$metadata" '^InfoKey: CreationDate')

                authorsArray=($(getAuthors "$author"))

                for auth in "${authorsArray[@]}";
                do
                    createFolders "$auth" "$creation" "$title" "$path" "$oldPath"
                done

                $1=$(($1+1)) #NOT SURE ABOUT THIS LINE
            fi
        fi
    done

    echo $1
}

indexCount=0
index $indexCount
+3
source share
1 answer

The correct syntax is:

var=$((var+1))

So instead

$1=$(($1+1))

you must use the variable name plus the syntax above. In general, remember that bash variables are set without $and are used with it.

Quote from Charles Duffy:

If bash targeting is unlike POSIX sh, there is also an option (( ++var ))or(( var += 1 ))

+2
source

All Articles