Bash script to encode images in base64 and generate a CSS file from them
cd "${BASH_SOURCE%/*}"

: &gt; "$css_path"



What happens if  cd fails?  Nothing good.  Make a habit of always, always testing the result of cd (and synonyms like pushd) for failure.   Bash's -e switch will promote all error exits to fatal script errors; a less extreme approach is simply:

cd … || exit 1





if [[ -z "$unsupported" &amp;&amp; -z "$invalid_class_name" ]]



This is testing the first elements of these two arrays for zero length.  You want to test the lengths of the arrays instead.  

When testing for zero, the use of arithmetic (( … )) is a nice touch: it returns truth for non-zero and false for zero.  

When a conditional includes an else block, avoid negating the condition.  In other words: prefer if x then yes else no  over if not x then no else yes.

 if (( ${#unsupported[@]} || ${#invalid_class_name[@]} )) 





filename="$(name_ext="${file##*/}"; echo "${name_ext%.*}")"
ext="${file##*.}"



This is awkward.  Most of the fault rests with Bash itself.  A regular expression is a little cleaner:

[[ $file =~ ([^/]*)\.([^.]*)$ ]] &amp;&amp; filename=${BASH_REMATCH[1]} ext=${BASH_REMATCH[2]}





if [[ "$mime" = "unsupported" ]]; then



Quotes aren't needed around variables (or literal strings that lack whitespace) inside [[ … ]].




  printf ".%s::before { background-image: url(data:%s;base64,%s); }\n" \
  "$filename" "$mime" "$base64str" \
  &gt;&gt; "$css_path"



It's good practice to indent continuations.  In the specific case of printf, it can be helpful to align variables with their format-string placeholders.




unsupported+=("$file")

invalid_class_name+=("$file")



If these exist in the shell that invokes your script, their initial values will persist and muck things up.  Make a habit of zeroing variables you intend to append to (or increment, etc.).  At the same time, improve readability by expressly declaring your arrays as such: 

declare -a unsupported=() invalid_class_name=()





: &gt; "$css_path"



This could be annoying if the script fails.  Consider taking a backup of the output file before zeroing it, and restoring the backup on error exit.