Alexander Ross

⏱ around 1 minutes read time

Tagged with:

Script with swift

If you are a swift developer you probably know already how to create an executable swift package or an command line tool using XCode. But then you also have to build the binary and place it in a executable path. Sometimes we just want to write a file and execute it as is. It's kind of common to do that with bash or ruby as small tools.

If we would create a bash file and execute it we could create a file named bashscript and add this content.

#!/usr/bin/env bash

echo "Hello, World!"

Then you need to make it executable with $ chmod u+x bashscript and now you can execute the file with $ ./bashscript.

But this is not limited to runtime languages. The magic commend with "#!" tells what program to use when evaluating the content of the file. So you can use that with swift as well.

Lets create a file called swiftscript and add this content.

#!/usr/bin/env swift

print("Hello, World!")

Then again, make it executable with $ chmod u+x swiftscript and now you can execute that script with $ ./swiftscript.

This is a great way to build small tools that you need and you can modify your script whenever you need without the need to manually recompile.

Tagged with: