Strange behavior of `hdiutil` declare -i s=${foo}; n=${bar}; mp=${baz}; cn=${qux}; mac_adr=${quux} 
sudo hdiutil create -size ${s}b -fs JHFS+X -volname "${n}" -type SPARSEBUNDLE \
"/Volumes/${mp}/${cn}_${mac_addr}.sparsebundle" &amp;&amp; echo "Done"


does not work, but

declare -i s=${foo}; n=${bar}; mp=${baz}; cn=${qux}; mac_adr=${quux} 
sudo hdiutil create -size ${s}b -fs JHFS+X -volname "${n}" -type SPARSEBUNDLE \
"/Volumes/${cn}_${mac_addr}.sparsebundle" &amp;&amp;
sudo mv -vn "/Volumes/${cn}_${mac_addr}.sparsebundle" "/Volumes/${mp}" &amp;&amp; echo "Done"


Works correctly

What am I doing wrong?
I used the following command in 10.9.5:

read -p "Size in GB? = " s; ((s= 10**9*s/512)); read -p "Name of image volume? = " n; read -p "External Volume name (Escape spaces with a backslash - the volume has to exist)? = " m; cn=`scutil --get ComputerName`; mac_addr=`ifconfig en0 | grep "ether"`; mac_addr=${mac_addr#*ether}; mac_addr=${mac_addr// /} ; mac_addr=${mac_addr//:/}; sudo hdiutil create -size ${s}b -fs JHFS+X -volname "${n}" -type SPARSEBUNDLE "/Volumes/${m}/${cn}_${mac_addr}.sparsebundle" &amp;&amp; echo "Done"


and it works (with a workaround for spaces in volume names like "Macintosh HD") for internal volumes:

Size in GB? = 2
Name of image volume? = aaa
Existing External Volume Name (Escape spaces with a backslash)? = test\ 2
created: /Volumes/test 2/host_001122334455.sparsebundle
Done
host:~ user$ 


This doesn't work if you create a sparsebundle on a network share. Here you mustn't use the sudo command. Instead use (I also simplified $mac_addr):

read -p "Size in GB? = " s; ((s= 10**9*s/512)); read -p "Name of image volume? = " n; read -p "External Volume name (Escape spaces with a backslash - the volume has to exist)? = " m; cn=`scutil --get ComputerName`; mac_addr=`ifconfig en0 | grep ether | awk '{print $2}' | sed 's/://g'`; hdiutil create -size ${s}b -fs JHFS+X -volname "${n}" -type SPARSEBUNDLE "/Volumes/${m}/${cn}_${mac_addr}.sparsebundle" &amp;&amp; echo "Done"