Celeb Glow
news | March 17, 2026

Allow filenames in bash associative array

I am using the following to fill fdir with directory names, whilst removing any duplicates. Would like to allow valid filenames as well.

declare -A tag
for dpa in "$@"; do [[ ! -d $dpa ]] && continue [[ ${tag[comint:$dpa]} ]] && continue fdir+=("$dpa") tag[comint:$dpa]=1
done
6

1 Answer

The condition [[ -d $dpa ]] || [[ -f $dpa ]] || continue caters for files as well.

declare -A tag
for dpa in "$@"; do [[ -d $dpa ]] || [[ -f $dpa ]] || continue [[ ${tag[comint:$dpa]} ]] && continue fdir+=("$dpa") tag[comint:$dpa]=1
done
1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy