How can I get a user's associated Apple ID from the command line?

I'd like to be able to retrieve the current user's associated Apple ID from a shell script. My first guess was to do so using dscl which I can get using my own user. But I'm not sure how reliable/consistent this method would be generally for any user.

dscl . readpl /Users/[username] dsAttrTypeNative:LinkedIdentity appleid.apple.com:linked\ identities:0:full\ name

Are there any alternative ways to do this? Maybe reading a plist somewhere using defaults read?


AppleIDs aren't specifically 'associated' with any given local user account - if there is an AppleID signed in on the Mac you might try using mas:

https://github.com/argon/mas

Specifically the command mas account should show you the currently logged-in AppleID.