I am trying to automate (using bash script)creation of JHFS+ formatted,DMG file on a macOS. The script should be able to intake user supplied:

  • size of the DMG in GB's
  • destination of the DMG
  • type of filesystem (HFS+,JHFS+,APFS,FAT32,ExFAT or UDF)
  • volume name
  • the name of the DMG to be created.

I used the following:

read -p "Enter the size of the DMG:   " size        
read -e -p "Enter the destination of the DMG:   " dest  
read -p "Enter the filesystem (HFS+,JHFS+,APFS,FAT32,ExFAT or UDF)  :" fs
read -p "Enter the Volume name :" volname
read -p "Enter the name of the DMG:" name

hdiutil create -fs {"$fs"} -size "$size" -volname "{$volname}" "{$dest\/$name}"

Issue is when the script is executed,with size mentioned as "1g" (1GB),I get the following error:

hdiutil: create failed - Invalid argument

Suggestions to improve the script are welcome.Thanks in advance :)


The curly brackets { and } around the variables are wrong and the escaped slash \/ doesn't work.

I changed $dest and $name to a combined $dest and added default values. For the size I added a little hint for the most common sizes.


defaults=( 1g HFS+ "my volume" ~/Desktop/myvolume.dmg )

read -ep "Enter the size (??m|??g|??t) [${defaults[0]}] " size 
read -ep "Enter the filesystem (HFS+, JHFS+, APFS, FAT32, ExFAT, UDF) [${defaults[1]}] " fs
read -ep "Enter the volume name [${defaults[2]}] " volname
read -ep "Enter the image destination [${defaults[3]}] " dest

hdiutil create -size "${size:-${defaults[0]}}" -fs "${fs:-${defaults[1]}}" -volname "${volname:-${defaults[2]}}" "${dest:-${defaults[3]}}"
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.