This process is very helpful to format a disk that has multiple partitions on it like a Raspberry Pi SD Card.
This script can be updated to allow a different type of format (JHFS+ instead of FAT32) but for now it serves my purpose.
#!/bin/sh # FormatSDCard.sh # # # Created by David Kittell on 6/11/19. # diskutil list | grep '/dev/disk' -A2 # Ask User what disk should be formatted # say "What disk would you like to format?. Copy/Paste or type in the disk path followed by [ENTER]" printf "What disk would you like to format?\nCopy/Paste or type in the disk path (ex. /dev/disk2) followed by [ENTER]:\n" read sDisk #echo "$sDisk provided" if [ ! -z "$sDisk" ] then sDiskSpace="$(diskutil list | grep $sDisk -A2 | grep -v $sDisk | grep -v 'SIZE' | tr -s " " | cut -d ' ' -f4 | sed 's|[+*]||g') $(diskutil list | grep $sDisk -A2 | grep -v $sDisk | grep -v 'SIZE' | tr -s " " | cut -d ' ' -f5)" echo $sDiskSpace clear # Ask User if they are sure they want to format the disk # say "Are you sure you want to format $sDisk ($sDiskSpace), if yes type y or if no type n followed by [ENTER]" printf "Are you sure you want to format $sDisk ($sDiskSpace), if yes type y or if no type n followed by [ENTER]:\n" read sDiskConfirm #echo "$sDiskConfirm" case $sDiskConfirm in [yY]) # Ask User if they are sure they want to format the disk # say "Do you want to do a secure format $sDisk ($sDiskSpace), if yes type y or if no (regular format) type n followed by [ENTER]" printf "Do you want to do a secure format $sDisk ($sDiskSpace), if yes type y or if no (regular format) type n followed by [ENTER]:\n" read sDiskFormatType #echo "$sDiskFormatType" case $sDiskFormatType in [yY]) sudo diskutil unmountDisk $sDisk && say "Unmount of disk completed" # sudo dd if=/dev/urandom of=$sDisk bs=1000000 && say "Disk Ready" sudo diskutil zeroDisk $sDisk && say "Disk Ready" sudo diskutil secureErase "ms-dos fat32" EMPTY MBRFormat $sDisk && say "Disk Is Formatted" # If you want to securely format the disk ;; [nN]) sudo diskutil unmountDisk $sDisk && say "Unmount of disk completed" # sudo dd if=/dev/urandom of=$sDisk bs=1000000 && say "Disk Ready" sudo diskutil zeroDisk $sDisk && say "Disk Ready" sudo diskutil eraseDisk "ms-dos fat32" EMPTY MBRFormat $sDisk && say "Disk Is Formatted" ;; *) clear # say "You did not provide a choice. Format of $sDisk ($sDiskSpace) aborted." echo "You did not provide a choice\nFormat of $sDisk ($sDiskSpace) aborted." ;; esac ;; [nN]) clear # say "Format of $sDisk ($sDiskSpace) aborted." echo "Format of $sDisk ($sDiskSpace) aborted." ;; *) clear # say "You did not confirm. Format of $sDisk ($sDiskSpace) aborted." echo "You did not confirm\nFormat of $sDisk ($sDiskSpace) aborted." ;; esac else clear # say "You did not provide a disk, aborting script" echo "You did not provide a disk, aborting script" fi