Scripting in Swift?

  Tutorials     August 19, 2015  

So, can Swift be used for Scripting, pretty much like any regular interpreted language – like PHP, Python, Ruby, Perl or… even Bash?

Well, here’s the catch: given its speed, it sure can – at least for smaller scripts. Plus, since Apple has already announced its future release as open-source software (which implies ports to Linux, Windows and the rest of the systems), this makes it a not so Mac-only thing.

So, the answer is: why not?

Our first “script”

Let’s get started then, with the same… ultra-complex “Hello World” we played with in the previous post.

And now let’s add the magic shebang line:

#!/usr/bin/env xcrun swift
print("Hello Swift!")

We save it as hello_script.swift. And that was it.

Executing it

Once we make it “officially” executable by:

sudo chmod +x hello_script.swift

We can now execute it as we would execute any script: