swift
terminal
programming
command-line
macOS

How can I use swift in Terminal?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

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:

bash
swift --version

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:

swift
1import Foundation
2
3let name = CommandLine.arguments.count > 1 ? CommandLine.arguments[1] : "world"
4print("Hello, \(name)!")

Run it with:

bash
swift hello.swift
swift hello.swift Taylor

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.

bash
swiftc hello.swift -o hello
./hello Taylor

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.

swift
1#!/usr/bin/env swift
2import Foundation
3
4print("Swift script running")

Then:

bash
chmod +x script.swift
./script.swift

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.

bash
mkdir MyTool
cd MyTool
swift package init --type executable

That creates a project structure with a manifest and source directory. Then run:

bash
swift run

Or build explicitly:

bash
swift build

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.

swift
1import Foundation
2
3for (index, arg) in CommandLine.arguments.enumerated() {
4    print("\(index): \(arg)")
5}

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 executable then swift 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 --version to 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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.