Executing Swift Code from Files in Ubuntu Linux as Script

In our previous article, we have seen how to setup Swift programming language in Ubuntu Linux and how to invoke Swift’s interactive shell to play with Swift language.

In this article, we will see how to run a Swift program saved in a file from Ubuntu Linux terminal as a script.

1. Open a text editor and type the given below Swift code and save to a file called hello.swift


2. Open a terminal and type the given below command

$swift hello.swift

3. Output

Now we can see an output as given below


4. Add the shebang line #!/usr/bin/env swift to the file hello.swift

#!/usr/bin/env swift

5. Make the file hello.swift to executable

$chmod ugo+x hello.swift

6. Run the file


Now we will get same output as in Step 3, but with out using swift command.

Comments on this post

No comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

Trackbacks and Pinbacks on this post

No trackbacks.

TrackBack URL