Обновленная версия: согласно полезным предложениям geirha.
Я избавился от массива, что было совершенно ненужным, и внесло изменения в то, как и какие глобальные настройки установлены / не установлены. Оригинальная версия по-прежнему включена; (для сравнения)
################################################
cd ~/ # create sample files with embedded spaces
touch note\ {1..3}
################################################
#
tdir="$HOME/notes"; # make target dir
[[ ! -d "$tdir" ]] && mkdir -p "$tdir"
#
shopt -q nullglob; Xnullglob=$? # state of nullglob
shopt -s nullglob # enable nullglob
shopt -s extglob # enable extglob
#
for f in note!(*.txt) ; do
if [[ -f $f ]] ; then
mv -i "$f" "$tdir/$f.txt"
fi
done
#
((Xnullglob==1)) && shopt -u nullglob # Reset nullglob
#
Оригинальная версия (с дополнительным треском):
cd ~/
# create some sample files with embedded spaces
touch note\ {1..3}
#
tdir="$HOME/notes"; # make target dir if not present
[[ ! -d "$tdir" ]] && mkdir -p "$tdir"
#
state=($(shopt extglob)) # Save extended globbing state
[[ ${state[1]} == off ]] && shopt -s extglob
#
farray=( note!(*.txt) ) # Build an array of filenames
fcount=${#farray[@]} # Get size of the array
#
for ((findex=0; findex<fcount; findex++));do
if [[ -f "${farray[findex]}" ]] ; then
echo -e $findex "${farray[findex]}"
mv -i "${farray[findex]}" \
"$tdir/${farray[findex]}.txt"
fi
done
#
[[ ${state[1]} == off ]] && shopt -u extglob # Reset extglob
#
# `mv -i` will interactively check with you before overwiting and existing file.
# You can use `mv -bf` to backup an existing file before overwriting it.