shrink a sparsebundle

To shrink a dynamically growing drive image (the parameter -stdinpass is need for encrypted images):

hdiutil compact -stdinpass file.sparseimage  

On a laptop running on battery power the parameter -batteryallowed is needed, otherwise there will be an error “function not implemented”.