How can I use swift in Terminal?
Codemia Knowledge Hub ·
Introduction
You can use Swift in Terminal in three main ways: run a script directly, compile a program with swiftc, or create a full Swift Package Manager project. The right choice depends on whether you want quick experimentation, a single-file tool, or a real command-line application with dependencies and tests.
Check That Swift Is Installed
First verify the toolchain is available:
If Swift is installed correctly, Terminal prints the version and target platform information.
Run A Swift Script Directly
For quick tasks, you can execute a .swift file without building a full package.
Create hello.swift:
Run it with:
This is the fastest way to use Swift for automation and small utilities.
Compile A Swift Program With swiftc
If you want a standalone executable, compile the source file.
This is better than interpreted script execution when you:
- want a reusable binary
- care about startup speed
- need to distribute the tool
For one-file command-line tools, swiftc is often enough.
Use A Shebang For Script-Like Tools
On Unix-like systems, you can make a Swift file executable as a script.
Then:
This makes Swift feel more like Python or Bash for small command-line utilities.
Build A Real CLI With Swift Package Manager
For anything beyond a tiny script, use Swift Package Manager.
That creates a project structure with a manifest and source directory. Then run:
Or build explicitly:
This is the standard route when you need:
- multiple source files
- dependencies
- tests
- a maintainable command-line app
Reading Command-Line Arguments
Swift exposes command-line arguments through CommandLine.arguments.
This works in scripts, compiled programs, and package-based executables.
REPL And Quick Experimentation
Depending on your installed toolchain and environment, interactive experimentation may also be available through Swift's command-line tools. But for most practical terminal workflows today, a short script file or a Swift package is the more dependable option.
In other words, do not wait for a perfect REPL workflow if what you really need is a ten-line script.
Common Terminal Workflows
A good mental model is:
- one-off script:
swift file.swift - reusable binary:
swiftc file.swift -o app - real project:
swift package init --type executablethenswift run
That covers almost every everyday terminal use case.
When Terminal Swift Is Especially Useful
Swift in Terminal is particularly handy for build scripts, file-processing utilities, quick API clients, and small automation tasks where using Xcode would add unnecessary overhead.
Common Pitfalls
- Assuming Swift only works inside Xcode. It works perfectly well from Terminal.
- Reaching for a full package when a one-file script would be enough.
- Using script mode for a larger tool that should really be built with Swift Package Manager.
- Forgetting to pass command-line arguments after the script name.
- Making a file executable with a shebang but forgetting
chmod +x.
Summary
- Use
swift --versionto confirm the Swift toolchain is installed. - Run quick scripts with
swift file.swift. - Compile standalone binaries with
swiftc. - Use Swift Package Manager for maintainable command-line applications.
- Swift in Terminal is practical for scripting, automation, and full CLI tool development.
Related reading
- How can I use Swift's Codable to encode into a dictionary?
- How can I use Tensorflow with react-native?
- How can I use Timer formerly NSTimer in Swift?
- How can I use UIColorFromRGB in Swift?
- How can I use UserDefaults in Swift?
- How can I write an iPhone app entirely in JavaScript without making it just a web app?
- How can my iphone app detect its own version number?
- How can we programmatically detect which iOS version is device running on?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.