Member-only story
10 Tips to Run Swift From Your Terminal
‘swift’, ‘swiftc’, and ‘lldb’
Sometimes when writing a block of Swift code, I don't bother to launch Xcode. Running the Swift commands swift
and swiftc
from the terminal are more lightweight and convenient. To debug and set a breakpoint, lldb
does the job.
1. ‘swift’ Command to Get the Swift Version
After installing the latest version of Swift from the download page, I’ll run the command to verify I’m running the expected Swift version:
$ swift --version

- It shows I’m currently on Swift 5.3
- The Swift/Objective-C compiler is on version 12
- The Swift environment is MacOS Catalina on a 64 bit CPU
2. Swift Command to Run the Swift File
Let’s using the vi
editor to write our first Swift file Foo.swift
to print out “Hello world!”
:
$ vi Foo.swift
Write down the foo()
function, and execute it:

To run the file, we use:
$ swift Foo.swift

3. Run the Swift File With Parameters
The CommandLine
enumeration is part of the Swift standard library to provide the command line arguments. We use CommandLine.arguments
to get the parameters after the swift
command.
To print out the “Hello, Foo!”
message, the code is:

To run the file with arguments, we use:
$ swift Foo.swift Foo