Running Swift Playground from the Command Line

Swift Playgrounds are stored as .playground bundles, i.e. directories with a particular hierarchy containing various files. Here's a typical Playground bundle created by XCode:

MyPlayground.playground
MyPlayground.playground/Contents.swift
MyPlayground.playground/contents.xcplayground
MyPlayground.playground/playground.xcworkspace
MyPlayground.playground/playground.xcworkspace/contents.xcworkspacedata
MyPlayground.playground/playground.xcworkspace/xcuserdata
MyPlayground.playground/playground.xcworkspace/xcuserdata/sveinbjorn.xcuserdatad
MyPlayground.playground/playground.xcworkspace/xcuserdata/sveinbjorn.xcuserdatad/UserInterfaceState.xcuserstate
MyPlayground.playground/timeline.xctimeline

To run a Swift Playground in the command line:

/usr/bin/swift MyPlayground.playground/Contents.swift