How do I pass a password to hdiutil attach?

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

and

echo test | hdiutil attach -stdinpass test.sparsebundle

But it returns

hdiutil: attach failed - Authentication error


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

So

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

works.