Shell Script Rename

http://stackoverflow.com –
Now I have the rename script like this:
cd /Users/KanZ/Desktop/Project/Test/
n=1
for file in *.jpg; do
echo $file
prefix=M
file_name=M$n.jpg
echo $file_name
n=$(( $n+1 ))
mv $file $file_name
done
The first if I run script, the all of jpg files will be M1.jpg M2.jpg M3.jpg but if I add the new file name A1.jpg to this directory and run script again. All of M1.jpg M2.jpg M3.jpg will be replaced by M4.jpg(before run script, this file named A1.jpg) because the first letter is A. It come before M. This is my problem. I would like to get M1 M2 M3 M4.jpg and every time
(HowTos)