Swift scripts (swift shebang), command-line tools
Create runnable Swift scripts with a shebang , read CommandLine.arguments , print help, and exit with proper status codes.
Why Swift scripts?
Swift can act as a scripting language. Use a shebang to run files directly, parse CommandLine.arguments, and exit with status codes for automation.
Shebang basics
Add #!/usr/bin/env swift as the first line, make the file executable, then run it like any shell script.
// First line (shebang) when saved to a file:
// #!/usr/bin/env swift
// Then mark file executable: chmod +x tool.swift
// Run directly: ./tool.swift
print("Hello from a Swift script!")All lessons in this course
- Swift scripts (swift shebang), command-line tools
- C/Obj-C interop at a glance (no iOS specifics)
- Packaging binaries with SPM