Bash to check if directory exist. If not create with an array

Just use:

mkdir -p -- "${array1[@]}"

That will also create intermediary directory components if need be so your array can also be shortened to only include the leaf directories:

array1=(
  /apache/bin
  /apache/conf
  /apache/lib
  /www/html
  /www/cgi-bin
  /www/ftp
)

Which you could also write:

array1=(
  /apache/{bin,conf,lib}
  /www/{html,cgi-bin,ftp}
)

The [[ -d ... ]] || mkdir ... type of approaches in general introduce TOCTOU race conditions and are better avoided wherever possible (though in this particular case it's unlikely to be a problem).


You have to loop in your array, then I would propose, in bash

array1=(
/apache
/apache/bin
/apache/conf
/apache/lib
/www
/www/html
/www/cgi-bin
/www/ftp
)
for dir in "${array1[@]}"; do
  [[ ! -d "$dir" ]] && mkdir "$dir"
done