control the sorting in for loop BASH

Solution 1:

A variation on Charles's answer:

shopt -s extglob
exception='NormalizeComponent'
dirs=( !("$exception")/ )
dirs=( "${dirs[0]}" "$exception"/ "${dirs[@]:1}" )
for d in "${dirs[@]}"; do
  echo "$d"
done

outputs

CloudUtils/
NormalizeComponent/
ComponentSplit/
ComponentStatistics/
ETLManager/
MergeComponent/
Reducer/

Solution 2:

You can control the sort results only insofar as there exists a collation order that matches what you need; LC_COLLATE can be used to pick the ordering to use -- but in this case, none of the ones your operating system ships will match the behavior you want.


You can use an extglob with a negative assertion to leave specific directories out of the glob's result, so you can hardcode the first and second items, and do everything else in the ordering the locale's collation settings provide:

#!/usr/bin/env bash
[ -n "$BASH_VERSION" ] || { echo "ERROR: only bash is supported" >&2; exit 1; }

shopt -s extglob
for d in CloudUtils NormalizeComponent !(CloudUtils|NormalizeComponent)/; do
    mvn clean deploy -f "${d%/}"
done