Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to update a plist file using bash. For some strings I can update the plist file, but for some strings I get an error. For eg

defaults write '/Users/test/Desktop/myPlist'  'My Key' "[My Value]"

throws an error

2011-08-10 18:49:36.208 defaults[17287:903] Could not parse: [My Value].
Try single-quoting it.

Looks like the characters '[' and ']' seem to be throwing it off. How to make this work with the string "[My Value]"?

share|improve this question
    
Maybe try single-quoting it...? – Wuffers Aug 11 '11 at 1:55

You could try single-quoting it, like this:

defaults write '/Users/test/Desktop/myPlist'  'My Key' "\"[My Value]\""
share|improve this answer
    
2011-08-10 19:05:44.531 defaults[17323:903] Could not parse: \'[My Value]\'. Try single-quoting it. Didnt work :( – smokinguns Aug 11 '11 at 2:02
    
This seemed to work: defaults write '/Users/test/Desktop/myPlist' 'My Key' "\"[My Value]\"" Wonder why? – smokinguns Aug 11 '11 at 2:04
    
Alright, will edit my answer. – Wuffers Aug 11 '11 at 2:12

None of the brackets and quotes are necessary and are not valid parts of a defaults write command. Here's a valid defaults write command that works in a bash script:

defaults write com.apple.Safari Homepage "http://google.com/"

here's a loop of defaults write commands I use to modify user templates on OS X:

for USER_TEMPLATE in "/System/Library/User Template"/*
do
    defaults write "${USER_TEMPLATE}"/Library/Preferences/com.apple.SetupAssistant DidSeeCloudSetup -bool TRUE
    defaults write "${USER_TEMPLATE}"/Library/Preferences/com.apple.desktopservices DSDontWriteNetworkStores true
    defaults write "${USER_TEMPLATE}"/Library/Preferences/.GlobalPreferences.plist AppleKeyboardUIMode -int 3
done

So, your example correctly formatted would look like this:

defaults write /Users/test/Desktop/myplist MyKey MyValue

of course you'll need to add the appropriate switches if you want the key to be written as something other than a string- see the examples above for boolean and integer keys being written.

Finally, check the man page for the defaults command- it does a great job of going through all of this.

share|improve this answer

You must log in to answer this question.

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