4

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]"?

1
  • Maybe try single-quoting it...?
    – Wuffers
    Aug 11, 2011 at 1:55

3 Answers 3

3

You could try single-quoting it, like this:

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

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.

0

You can single quote escape your brackets by wrapping them in a double quote:

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

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

Notice the single quotes added to [My Value]

You must log in to answer this question.

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