How do I pass a password to hdiutil attach?

I have an encrypted sparsebundle (test.sparsebundle) and it's password is "test", with no quotes.

I'd like to automate opening that file.

I have tried

echo "test" | hdiutil attach -stdinpass test.sparsebundle


echo test | hdiutil attach -stdinpass test.sparsebundle

But it returns

hdiutil: attach failed - Authentication error

1 Answer 1


Ah! A little more digging, and careful reading of the man page. I need a null terminated string.


printf '%s\0' 'test' | hdiutil attach test.sparsebundle -stdinpass


  • 1
    If the password has no spaces or characters special to the shell it can be shortened to just: printf test|hdiutil attach test.sparsebundle -stdinpass Aug 2, 2015 at 16:35
  • 1
    The \0 isn't actually needed, but using %s is a good idea -- there are a lot of "special characters" that can trip you up. Aug 2, 2015 at 17:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .