Last updated 1 min read

How to Run Swift Script From CLI

#Swift

Swift code can be run directly from CLI without need to prior compilation. This is very helpful for scripting and writing some tooling.

All you need is to add on top of the .swift file:

#!/usr/bin/swift

Then swift file can be run from CLI:

swift main.swift

Alternatively, you can make the script file executable:

chmod +x main.swift

and run it:

./main.swift

References