How can I use swift in Terminal?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

